File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Statement: Sort an array with quicksort and try different partitioning strategies.
3+ Input: List of Ints; may be empty, singleton or very large
4+ Out: List of Ints
5+ -----------
6+ Understand:
7+ - Problem statement calls for quicksort.
8+ - Quicksort (wikipedia):
9+ 1. Pick an element, called a pivot, from the array.
10+ 2. Partitioning: reorder the array so that all elements with values less than
11+ the pivot come before the pivot, while all elements with values greater than
12+ the pivot come after it (equal values can go either way). After this partitioning,
13+ the pivot is in its final position. This is called the partition operation.
14+ 3. Recursively apply the above steps to the sub-array of elements with smaller
15+ values and separately to the sub-array of elements with greater values.
16+ - We should be able to easily change which partitioning strategy we use in the implementation.
17+ - Skiena points out that some inputs (which?) can be pathological, but that pre-shuffling our input takes care of most of these
18+ """
19+
20+
21+
22+ def lomuto_partition ():
23+ pass
24+
25+ def hoare_partition ():
26+ pass
27+
28+ def sort (arr , partition_fn ):
29+ return arr
30+
31+ def quicksort (arr , partition_fn , shuffle = True ):
32+ if shuffle :
33+ # do shuffling here
34+ pass
35+ return sort (arr , partition_fn )
36+
37+ if __name__ == '__main__' :
38+ test_arrs = [
39+ [],
40+ [1 ]
41+ [6 , 5 , 4 , 3 , 2 , 1 ],
42+ [1 , 2 , 3 , 4 , 5 , 6 ]
43+ [1 , 1 , 1 , 1 , 1 , 1 ],
44+ [5 , 1 , 3 , 4 , 5 , 10 ],
45+ ]
46+ for arr in test_arrs :
47+ assert quicksort (arr , lomuto_partition ) == sorted (arr )
48+ assert quicksort (arr , hoare_partition ) == sorted (arr )
You can’t perform that action at this time.
0 commit comments