Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2b5641a

Browse files
author
codebasics
committedSep 20, 2020
quick sort
1 parent 678738e commit 2b5641a

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
 

‎Algorithms/3_QuickSort/quick_sort.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# implementation of quick sort in python using hoare partition scheme
2+
3+
def swap(a, b, arr):
4+
if a!=b:
5+
tmp = arr[a]
6+
arr[a] = arr[b]
7+
arr[b] = tmp
8+
9+
def quick_sort(elements, start, end):
10+
if start < end:
11+
pi = partition(elements, start, end)
12+
quick_sort(elements, start, pi-1)
13+
quick_sort(elements, pi+1, end)
14+
15+
def partition(elements, start, end):
16+
pivot_index = start
17+
pivot = elements[pivot_index]
18+
19+
while start < end:
20+
while start < len(elements) and elements[start] <= pivot:
21+
start+=1
22+
23+
while elements[end] > pivot:
24+
end-=1
25+
26+
if start < end:
27+
swap(start, end, elements)
28+
29+
swap(pivot_index, end, elements)
30+
31+
return end
32+
33+
34+
if __name__ == '__main__':
35+
elements = [11,9,29,7,2,15,28]
36+
# elements = ["mona", "dhaval", "aamir", "tina", "chang"]
37+
quick_sort(elements, 0, len(elements)-1)
38+
print(elements)
39+
40+
tests = [
41+
[11,9,29,7,2,15,28],
42+
[3, 7, 9, 11],
43+
[25, 22, 21, 10],
44+
[29, 15, 28],
45+
[],
46+
[6]
47+
]
48+
49+
for elements in tests:
50+
quick_sort(elements, 0, len(elements)-1)
51+
print(f'sorted array: {elements}')
52+
53+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### Exercise: Quick Sort
2+
Implement quick sort using lumoto partition scheme. This partition scheme is explained in the video tutorial, you need to write python code to implement it.
3+
Check the pseudo code here: https://en.wikipedia.org/wiki/Quicksort Check the section Lomuto partition scheme
4+
5+
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/3_QuickSort/quick_sort_exercise_soluiton_lomuto.py)
6+
7+
8+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This implements quick sort using lomuto partition scheme
2+
3+
def swap(a, b, arr):
4+
if a!=b:
5+
tmp = arr[a]
6+
arr[a] = arr[b]
7+
arr[b] = tmp
8+
9+
def quick_sort(elements, start, end):
10+
if len(elements)== 1:
11+
return
12+
if start < end:
13+
pi = partition(elements, start, end)
14+
quick_sort(elements, start, pi-1)
15+
quick_sort(elements, pi+1, end)
16+
17+
def partition(elements, start, end):
18+
pivot = elements[end]
19+
p_index = start
20+
21+
for i in range(start, end):
22+
if elements[i] <= pivot:
23+
swap(i, p_index, elements)
24+
p_index += 1
25+
26+
swap(p_index, end, elements)
27+
28+
return p_index
29+
30+
31+
if __name__ == '__main__':
32+
tests = [
33+
[11,9,29,7,2,15,28],
34+
[3, 7, 9, 11],
35+
[25, 22, 21, 10],
36+
[29, 15, 28],
37+
[],
38+
[6]
39+
]
40+
# elements = ["mona", "dhaval", "aamir", "tina", "chang"]
41+
42+
for elements in tests:
43+
quick_sort(elements, 0, len(elements)-1)
44+
print(f'sorted array: {elements}')

0 commit comments

Comments
 (0)
Please sign in to comment.