Skip to content

Commit 6441406

Browse files
committed
QuickSort.py finally approved
1 parent cbc62f7 commit 6441406

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def partition(unsorted_array, first_index, last_index):
2+
pivot = unsorted_array[first_index]
3+
pivot_index = first_index
4+
index_of_last_element = last_index
5+
less_than_pivot_index = index_of_last_element
6+
greater_than_pivot_index = first_index + 1
7+
8+
while True:
9+
while unsorted_array[greater_than_pivot_index] < pivot and greater_than_pivot_index < last_index:
10+
greater_than_pivot_index += 1
11+
while unsorted_array[less_than_pivot_index] > pivot and less_than_pivot_index >= first_index:
12+
less_than_pivot_index -=1
13+
if greater_than_pivot_index < less_than_pivot_index:
14+
temp = unsorted_array[greater_than_pivot_index]
15+
unsorted_array[greater_than_pivot_index] = unsorted_array[less_than_pivot_index]
16+
unsorted_array[less_than_pivot_index] = temp
17+
else:
18+
break
19+
unsorted_array[pivot_index] = unsorted_array[less_than_pivot_index]
20+
unsorted_array[less_than_pivot_index] = pivot
21+
return less_than_pivot_index
22+
23+
24+
def quicksort(unsorted_array, first, last):
25+
if last - first <= 0:
26+
return
27+
else:
28+
partition_point = partition(unsorted_array, first, last)
29+
quicksort(unsorted_array, first, partition_point - 1)
30+
quicksort(unsorted_array, partition_point + 1, last)
31+
32+
33+
my_array = [43, 3, 77, 89, 4, 20]
34+
print(my_array)
35+
quicksort(my_array, 0, 5)
36+
print(my_array)

0 commit comments

Comments
 (0)