MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / quicksort

Function quicksort

tools/python-3.11.9-amd64/Tools/demo/sortvisu.py:414–473  ·  view source on GitHub ↗
(array)

Source from the content-addressed store, hash-verified

412 array.message("Sorted")
413
414def quicksort(array):
415 size = array.getsize()
416 array.reset("Quicksort")
417 try:
418 stack = [(0, size)]
419 while stack:
420 first, last = stack[-1]
421 del stack[-1]
422 array.show_partition(first, last)
423 if last-first < 5:
424 array.message("Insertion sort")
425 for i in range(first+1, last):
426 j = i-1
427 while j >= first:
428 if array.compare(j, j+1) <= 0:
429 break
430 array.swap(j, j+1)
431 j = j-1
432 continue
433 array.message("Choosing pivot")
434 j, i, k = first, (first+last) // 2, last-1
435 if array.compare(k, i) < 0:
436 array.swap(k, i)
437 if array.compare(k, j) < 0:
438 array.swap(k, j)
439 if array.compare(j, i) < 0:
440 array.swap(j, i)
441 pivot = j
442 array.show_pivot(pivot)
443 array.message("Pivot at left of partition")
444 array.wait(1000)
445 left = first
446 right = last
447 while True:
448 array.message("Sweep right pointer")
449 right = right-1
450 array.show_right(right)
451 while right > first and array.compare(right, pivot) >= 0:
452 right = right-1
453 array.show_right(right)
454 array.message("Sweep left pointer")
455 left = left+1
456 array.show_left(left)
457 while left < last and array.compare(left, pivot) <= 0:
458 left = left+1
459 array.show_left(left)
460 if left > right:
461 array.message("End of partition")
462 break
463 array.message("Swap items")
464 array.swap(left, right)
465 array.message("Swap pivot back")
466 array.swap(pivot, right)
467 n1 = right-first
468 n2 = last-left
469 if n1 > 1: stack.append((first, right))
470 if n2 > 1: stack.append((left, last))
471 array.message("Sorted")

Callers

nothing calls this directly

Calls 12

show_partitionMethod · 0.80
show_pivotMethod · 0.80
show_rightMethod · 0.80
show_leftMethod · 0.80
hide_partitionMethod · 0.80
getsizeMethod · 0.45
resetMethod · 0.45
messageMethod · 0.45
compareMethod · 0.45
swapMethod · 0.45
waitMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected