Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9cd30ee

Browse files
author
Amogh Singhal
authored
Create quick_sort.py
1 parent 12ba921 commit 9cd30ee

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎quick_sort.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def swap(a, b):
2+
temp = a
3+
a = b
4+
b = temp
5+
6+
def quick_sort(array):
7+
quick_sort_helper(array, 0, len(array)-1)
8+
9+
def quick_sort_helper(arr, first, last):
10+
if first < last:
11+
split_point = partition(arr, first, last)
12+
13+
quick_sort_helper(arr, first, split_point - 1)
14+
quick_sort_helper(arr, split_point + 1, last)
15+
16+
def partition(arr, first, last):
17+
pivot = arr[first]
18+
19+
left = first + 1
20+
right = last
21+
22+
done = False
23+
24+
while not done:
25+
26+
# move left and right pointers to locate split_point
27+
while left <= right and arr[left] <= pivot:
28+
left += 1
29+
while arr[right] >= pivot and right >= left:
30+
right -= 1
31+
32+
# when both pointers cross each other
33+
# we have located the split_point
34+
if right < left:
35+
done = True
36+
else:
37+
# swap the left and right elements
38+
swap(arr[left], arr[right])
39+
40+
# swap the first and right elements
41+
swap(arr[first], arr[right])
42+
43+
return right
44+
45+
array = [54,26,93,17,77,31,44,55,20]
46+
quick_sort(array)
47+
print(array)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /