-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.pp
68 lines (53 loc) · 1.35 KB
/
quickSort.pp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
def partition(myList, pleft, pright):
j = pleft
t = pright
pivot = myList[j]
while j < t:
if myList[j+1] <= pivot:
temp = myList[j+1]
myList[j+1] = myList[j]
myList[j] = temp
j = j+1
else:
temp = myList[j+1]
myList[j+1] = myList[t]
myList[t] = temp
t = t-1
return j
def quickSort(myList, left, right):
if (right-left) < 1:
return
j = partition(myList, left, right)
quickSort(myList, left, j-1)
quickSort(myList, j+1, right)
def main():
while True:
try:
val = int(input("Enter the number of values you would like to sort: "))
break
except ValueError:
print("Invalid input. Please enter a different value.")
numList = []
numCount = 0
while numCount < val:
x = random.randint(0, 100)
numList.append(x)
numCount = numCount + 1
quickSort(numList, 0, numCount-1)
print("Sorted values:")
for x in range(val):
print(numList[x])
while True:
try:
val1 = int(input("Would you like to sort a different set of values? (enter 1 is yes or enter 0 if no): "))
break
except ValueError:
print("Invalid input. Please enter a different value.")
while val1 != 0 and val1 != 1:
val1 = int(input("Invalid input. Please re-enter value (enter 1 if yes or enter 0 if no): "))
if val1 == 1:
main()
if val1 == 0:
quit()
main()