forked from thundergolfer/interview-with-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-sort.py
More file actions
116 lines (100 loc) · 3.44 KB
/
quick-sort.py
File metadata and controls
116 lines (100 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from random import randint
import unittest
from math import ceil
def quickSort(lst):
# List of 0 or 1 items is already sorted
if len(lst) <= 1:
return lst
else:
# Pivot can be chosen randomly
pivotIndex = randint(0, len(lst)-1)
pivot = lst[pivotIndex]
# Elements lower than and greater than pivot
lesser, greater = [], []
for index in range(len(lst)):
# Don't do anything if you're at the pivot
if index == pivotIndex:
pass
else:
# Sort elements into < pivot and >= pivot
el = lst[index]
if el < pivot:
lesser.append(el)
else:
greater.append(el)
# Sort lesser and greater, concatenate results
return quickSort(lesser) + [pivot] + quickSort(greater)
def quickSort_two(lst):
"""
This algorithm is intuitive, but does not map closely to the lower-level
Java and C++ implementations of quickSort.
"""
if len(lst) <= 1:
return lst
pivotIndx = len(lst) - 1 # put pivot at end
pivot = lst[pivotIndx]
# Elems lower than and greater than pivot
lesser, equal, greater = [], [], []
for i in range(len(lst)):
if i == pivot:
equal.append(x)
elif i < pivot:
less.append(x)
elif i > pivot:
greater.append(x)
return quickSort_two(less) + equal + quickSort_two(greater) # just use + to join lists
# def quickSort_three(lst):
# def quickSort_partition(lst, low, high ):
# pivot = lst[ceil(low + (high-low)/2)]
# i, j = low, high
# while i <= j:
# # If the curr value from the left list is smaller then the pivot
# # elem then get the next element from the left list
# while lst[i] < pivot:
# i += 1
# # If the current value from the right list is larger than the pivot
# # elem then get the next element from the right list
# while lst[high] > pivot:
# high -= 1
#
# # If we have found a value in the left list which is larger than
# # the pivot element and if we have found a vluae int the right list
# # which is smaller than the pivot element then we exchange the values.
# if i <= high:
# exchange(lst, i, high)
# i += 1
# high += 1
# if low < j:
# quickSort_partition(lst, low, j)
# if i < high:
# quickSort_partition(lst, i, high)
# return lst
#
# def exchange(lst, i, j):
# temp = lst[i]
# lst[i] = lst[j]
# lst[j] = temp
# return lst
# # Algorthim body
# if len(lst) <= 1:
# return lst
# quickSort_partition(lst, 0, len(lst) - 1)
#
class Test_QuickSort(unittest.TestCase):
def setUp(self):
pass
def test_quickSort_one(self):
self.assertEqual(
quickSort([1,4,3,5,9,8,2,6,7]), [1,2,3,4,5,6,7,8,9]
)
self.assertEqual( quickSort([]), [] )
self.assertEqual( quickSort([2]), [2])
self.assertEqual( quickSort([1,2,3,4,5,6]), [1,2,3,4,5,6])
def test_quickSort_two(self):
pass
# def test_quickSort_three(self):
# self.assertEqual(
# quickSort_three([1,2,3,4,5]), [1,2,3,4,5]
# )
if __name__ == '__main__':
unittest.main()