-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting Algorithms.py
182 lines (137 loc) · 3.94 KB
/
Sorting Algorithms.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import math
from numpy import random
from time import time
def insertion_sort (l):
for i in range(1, len(l)):
key = l[i]
j = i-1
while(j>=0 and l[j]>key):
l[j+1] = l[j]
j -= 1
l[j+1] = key
return l
def selection_sort(l):
for i in range (len(l)):
min_ = i
for j in range (i,len(l)):
if(l[j] < l[i]):
min_ = j
print("before",i,l)
temp = l[min_]
l[min_] = l[i]
l[i] = temp
print("after ",i,l)
print()
return l
def bubble_sort(l):
for i in range(len(l)):
swap = False
for j in range (len(l)-1):
if(l[j] > l[j+1]):
swap = True
temp = l[j]
l[j] = l[j+1]
l[j+1] = temp
if(swap == False):
break
return l
a=[2,1,3,4,5,6,7]
#a = max_subarray(a)
#print(a)
def max_crossing_subarray(a,low,high,mid):
left_sum = -99999999999
sum_ = 0
left = 0
for i in range(mid, low - 1, -1):
sum_ = sum_ + a[i]
if(sum_ > left_sum):
left_sum = sum_
left = i
right_sum = -99999999999
sum_ = 0
right = 0
for i in range((mid + 1), (high + 1)):
sum_ = sum_ + a[i]
if(sum_ > right_sum):
right_sum = sum_
right = i
return left,right, (left_sum + right_sum)
def max_subarray_greedy (a, low, high):
if(low == high):
return low,high,a[0]
else:
mid = math.floor((low + high)/2)
left_low, left_high, left_sum = max_subarray_greedy(a, low, mid)
right_low, right_high, right_sum = max_subarray_greedy(a, mid+1, high)
cross_low, cross_high, cross_sum = max_crossing_subarray(a, low, high, mid)
if(left_sum >= right_sum and left_sum > cross_sum):
return (left_low, left_high, left_sum)
elif(right_sum >= left_sum and right_sum > cross_sum):
return (right_low, right_high, right_sum)
else:
return (cross_low, cross_high, cross_sum)
def max_subarray(l):
max_ = -9999999999
low = 0
high = 0
for i in range(len(l)-1):
s = l[i]
for j in range ((i+1),len(l)):
s = s + l[j]
if(s > max_):
max_ = s
low = i
high = j
return low, high, max_
"""
a=[]
for i in range(30000):
a.append(random.randint(-100,100))
x = time()
print(max_subarray_greedy(a,0,len(a)-1))
print("dc",time() - x)
x = time()
print(max_subarray(a))
print("ndc",time() - x)
#print(a)
"""
def merge (left, right):
a=[]
n=0
l=0
r=0
flag = 0
marker = []
limit = len(left) + len(right)
while(n < limit ):
if(left[l] <= right[r] ):
a.append(left[l])
if(l+1 != len(left)):
l+=1
else:
flag = right
marker = r
break
else:
a.append(right[r])
if(r+1 != len(right)):
r+=1
else:
flag = left
marker = l
break
if(len(a) != limit):
for i in range (marker, len(flag)):
a.append(flag[marker])
marker +=1
return a
def merge_sort(a, low, high):
if (low >= high):
c = [a[low]]
return c
else:
mid = math.floor((low + high)/2)
return merge(merge_sort (a, low, mid), merge_sort (a, mid + 1, high))
a = [10,9,8,7,6,5,4,4,3,2,1,0,-1]
print (merge_sort(a, 0, len(a)-1))
#print(merge([1,2,3,4,5], [6,7,8,9,10]))