-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.py
More file actions
32 lines (29 loc) · 857 Bytes
/
Copy pathquicksort.py
File metadata and controls
32 lines (29 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Quick Sort
def swap(arr, a, b):
temp = arr[a]
arr[a] = b
arr[b] = temp
def quicksort(arr, start, end):
if start < end and (end - start) > 1:
pivot = start
# print ("start", start, "pivot", pivot, "end", end, "arr", arr[start:end])
# swap(arr, start, pivot)
ltr = start + 1
rtl = end
while ltr < rtl:
while arr[start] > arr[ltr]:
ltr += 1
while arr[start] < arr[rtl]:
rtl -= 1
if ltr < rtl:
swap(arr, ltr, rtl)
ltr += 1
rtl -= 1
# swap(arr, start, rtl)
quicksort(arr, start, pivot-1)
quicksort(arr, pivot+1, end)
def sort(arr):
quicksort(arr, 0, len(arr)-1)
return arr
inp = [int(x) for x in input().split(' ')]
print(sort(inp))