-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.py
More file actions
96 lines (88 loc) · 2.36 KB
/
Sort.py
File metadata and controls
96 lines (88 loc) · 2.36 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def Merge(a, lowerBound, mid, upperBound):
k = lowerBound
i = lowerBound
j = mid+1
m = upperBound
b = []
b = [0]*len(a)
while i <= mid and j <= upperBound:
if a[i] < a[j]:
b[k] = a[i]
i += 1
else:
b[k] = a[j]
j += 1
k += 1
while i <= mid:
b[k] = a[i]
i += 1
k += 1
while j <= upperBound:
b[k] = a[j]
j += 1
k += 1
for z in range(lowerBound, upperBound+1):
a[z] = b[z]
def mergeSort(a, lowerBound, upperBound):
if lowerBound < upperBound:
mid = (upperBound+lowerBound)//2
mergeSort(a, lowerBound, mid)
mergeSort(a, mid+1, upperBound)
Merge(a, lowerBound, mid, upperBound)
def quickSort(a, lowerBound, upperBound):
if upperBound > lowerBound:
pivot = lowerBound
i = lowerBound
j = upperBound
while i < j:
while a[i] <= a[pivot] and i < upperBound:
i += 1
while a[j] > a[pivot]:
j -= 1
if i < j:
temp = a[i]
a[i] = a[j]
a[j] = temp
temp = a[pivot]
a[pivot] = a[j]
a[j] = temp
quickSort(a, lowerBound, j-1)
quickSort(a, j+1, upperBound)
a = []
while True:
choice = int(input('''Enter 1: for Input
2: for QuickSort
3: for MergeSort
4: for Display
5: Exit
=>'''))
if choice == 1:
size = int(input("Enter size of Array: "))
if size <= 0:
print("Invalid Array size!\nTry Again")
continue
print("Enter the Values")
for i in range(size):
Input = int(input())
a.append(Input)
elif choice == 2:
if not a:
print("Array is Empty")
else:
quickSort(a, 0, size-1)
print("Sorting done! (QuickSort)")
elif choice == 3:
if not a:
print("Array is Empty")
else:
mergeSort(a, 0, size-1)
print("Sorting done! (MergeSort)")
elif choice == 4:
if not a:
print("Array is Empty")
else:
print(a)
elif choice == 5:
exit()
else:
print("Wrong Choice")