Skip to content

Commit d8b880f

Browse files
committed
quicksort
1 parent 80a8161 commit d8b880f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

array_quickSort.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def quickSort(arr):
2+
qshelper(arr, 0, len(arr) - 1)
3+
return arr
4+
5+
6+
def qshelper(arr, start, end):
7+
if start >= end:
8+
return
9+
10+
pivot = start
11+
left = start + 1
12+
right = end
13+
14+
while right >= left:
15+
16+
if arr[left] > arr[pivot] and arr[right] < arr[pivot]:
17+
arr[left], arr[right] = arr[right], arr[left]
18+
19+
if arr[left] <= arr[pivot]:
20+
left += 1
21+
22+
if arr[right] >= arr[pivot]:
23+
right -= 1
24+
25+
arr[right], arr[pivot] = arr[pivot], arr[right]
26+
print(arr)
27+
qshelper(arr, start, right - 1)
28+
qshelper(arr, right + 1, end)
29+
30+
31+
arr = [50, 40, 30, 20, 10]
32+
print('steps of sorting:')
33+
quickSort(arr)
34+
print('After sorting :', arr)
35+

0 commit comments

Comments
 (0)