-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsortingAlgorithms.py
112 lines (95 loc) · 2.8 KB
/
sortingAlgorithms.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#Algorithm 1: Quick sort with random pivot-O(n) time for average scenario,O(n^2) in the worst case
import random
def pivotPlace(a,first,last):
randomIndex=rand.randint(first,last)
a[randomIndex],a[last]=a[last],a[randomIndex]
pivot=a[last]
left=first
right=last-1
while True :
while left<=right and a[left]<=pivot :
left=left+1
while left<=right and a[right]>=pivot :
right=right-1
if right<left :
break
else :
a[left],a[right]=a[right],a[left]
a[last],a[left]=a[left],a[last]
return left
def quickSort(wireHashMap,first,last):
a= [k for k in wireHashMap]
p=pivotPlace(a,first,last)
quickSort(wireHashMap,first,p-1)
quickSort(wireHashMap,p+1,last)
sortedWireHashMap={}
for i in a:
sortedWireHashMap[i]=wireHashMap[i]
return sortedWireHashMap
# To execute just input the wireHashMap in the main function and type result=quickSort(wireHashMap,0,len(wireHashMap)-1)
#Algorithm 2:Heap sort-O(nlogn) in the worst case scenario
def maxheapify(a, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and a[largest] < a[l]:
largest = l
if r < n and a[largest] < a[r]:
largest = r
if largest != i:
a[i],a[largest] = a[largest],a[i]
maxheapify(a, n, largest)
def heapSort(wireHashMap):
n = len(wireHashMap)
a= [k for k in wireHashMap]
for i in range(n, -1, -1):
maxheapify(a, n, i)
for i in range(n-1, 0, -1):
a[i], a[0] = a[0], a[i]
maxheapify(a, i, 0)
sortedWireHashMap={}
for i in a:
sortedWireHashMap[i]=wireHashMap[i]
return sortedWireHashMap
#The execution is the same as quicksort but here you should call the heapSort function
#Algorithm 3:mergeSort -O(nlogn) in the worst case scenario
def merge(a, low, pivot, high):
n1 = pivot - low + 1
n2 = high - pivot
L = [0] * (n1)
R = [0] * (n2)
for i in range(0 , n1):
L[i] = a[low + i]
for j in range(0 , n2):
R[j] = a[pivot + 1 + j]
i = 0
j = 0
k = low
while i < n1 and j < n2 :
if L[i] <= R[j]:
a[k] = L[i]
i += 1
else:
a[k] = R[j]
j += 1
k += 1
while i < n1:
a[k] = L[i]
i += 1
k += 1
while j < n2:
a[k] = R[j]
j += 1
k += 1
def mergeSort(wireHashMap,l,r):
a=[k for k in wireHashMap]
if l < r:
m = (l+(r-1))/2
mergeSort(wireHashMap, l, m)
mergeSort(wireHashMap, m+1, r)
merge(a, l, m, r)
sortedWireHashMap={}
for i in a:
sortedWireHashMap[i]=wireHashMap[i]
return sortedWireHashMap
#The execution is similar to other algorithms