Skip to content

Commit 2ef485c

Browse files
author
Zidong Zhang
committedNov 12, 2018
update quicksort.py
1 parent d41b451 commit 2ef485c

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed
 

‎Python_utils ‎Python_utils.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
1) List
2+
len(lst)
3+
lst[-1]
4+
lst1 + lst2
5+
cmp(list1, list2)
26
sorted(lst)
37
lst.sort()
48
lst.append(e)
@@ -21,9 +25,9 @@
2125
print ''.join(['h','i'])
2226
print ord('a')
2327
print '1 2'.split()
24-
28+
2529
4) if __name__ == '__main__':
26-
5)
30+
5)
2731
6) for i in range(len(lst)-1,-1,-1):
2832
7) {char:[] for char in Set}
2933
8) min(lst, key=foo)
@@ -48,4 +52,4 @@ except:
4852
# 17) from collections import deque
4953
queue = deque([1,2,3])
5054
queue.append(4)
51-
queue.popleft() # FIFO
55+
queue.popleft() # FIFO

‎Util/quicksort.py

+53-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,66 @@
1+
import unittest
2+
3+
14
# sort a list of numbers
25
def quicksort(lst):
3-
if len(lst)<=1:
4-
return lst
5-
6-
pivot = lst[0]
7-
left,right = partition(lst[1:],pivot)
8-
return quicksort(left) + [pivot] + quicksort(right)
6+
if len(lst) <= 1:
7+
return lst
8+
9+
pivot = lst[0]
10+
left, right = partition(lst[1:], pivot)
11+
return quicksort(left) + [pivot] + quicksort(right)
12+
913

1014
# lenght of lst is at least 1
11-
def partition(lst,pivot):
15+
def partition(lst, pivot):
1216
left = 0
1317
right = 0
1418
while right != len(lst):
1519
if lst[right] < pivot:
16-
lst[left],lst[right] = lst[right],lst[left]
20+
lst[left], lst[right] = lst[right], lst[left]
1721
left += 1
1822
right += 1
19-
return lst[:left],lst[left:]
23+
return lst[:left], lst[left:]
24+
25+
26+
# the orginal lst remains unchanged after calling this method
27+
def lomuto_quicksort_not_in_place(lst):
28+
if len(lst) <= 1:
29+
return lst
30+
pivot, lst1, lst2 = lst[-1], [], []
31+
for e in lst[:-1]:
32+
if e <= pivot:
33+
lst1.append(e)
34+
else:
35+
lst2.append(e)
36+
sorted_lst1 = lomuto_quicksort_not_in_place(lst1)
37+
sorted_lst2 = lomuto_quicksort_not_in_place(lst2)
38+
return sorted_lst1 + [pivot] + sorted_lst2
39+
40+
41+
class TestQuicksort(unittest.TestCase):
42+
# return a list of tuples (lst_unsorted, expected)
43+
def data_provider(self):
44+
return [
45+
([], []),
46+
([1], [1]),
47+
([1, 2], [1, 2]),
48+
([2, 1], [1, 2]),
49+
([3, 2, 1], [1, 2, 3]),
50+
([1, 3, 2, 1, 2], [1, 1, 2, 2, 3]),
51+
([6, 0, 8, 8, 8, 6, 7, 3, 3, 6], [0, 3, 3, 6, 6, 6, 7, 8, 8, 8]),
52+
]
53+
54+
# Date: 2015
55+
def test_quicksort(self):
56+
for lst_unsorted, expected in self.data_provider():
57+
self.assertEqual(quicksort(lst_unsorted), expected)
58+
59+
# Date: Nov 11, 2018
60+
def test_lomuto_quicksort_not_in_place(self):
61+
for lst_unsorted, expected in self.data_provider():
62+
self.assertEqual(lomuto_quicksort_not_in_place(lst_unsorted), expected)
2063

2164

2265
if __name__ == '__main__':
23-
print quicksort([6,9,2,9,10,6])
24-
print quicksort([2,1])
66+
unittest.main()

0 commit comments

Comments
 (0)