-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsa_sorting.py
167 lines (111 loc) · 3.92 KB
/
dsa_sorting.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
import time
### Bubble Sort Algorithm
def bubble_sort(array):
n = len(array)
for i in range(n):
all_sorted = True # flag to terminate if nothing left to sort
# Look at each item of the list one by one,
# comparing it with its adjacent value.
# After each iteration, the portion of the array to sort shrinks
# because the remaining items have already been sorted.
for j in range(n - i - 1):
if array[j] > array[j + 1]:
# If the item is greater than its adjacent, then swap them
array[j], array[j + 1] = array[j + 1], array[j]
# As long as sorting happens, 'all_sorted' is set to False
# so the algorithm doesn't stop
all_sorted = False
print(array)
# If there were no swaps during the last iteration,
# the array is already sorted, and algorithm can terminate
if all_sorted:
break
return array
a = [8, 2, 6, 5, 4]
[*range(len(a))]
start_time = time.time()
bubble_sort(a)
time.time() - start_time
### Insertion Sort Algorithm
def insertion_sort(array):
# Loop from the second element of the array until the last element
for i in range(1, len(array)):
# Element to be positioned
item = array[i]
# Initialize the position variable
j = i - 1
# Run through the list of items (the left subarray)
# and find the correct position of the item.
# Do this only if item is smaller than its adjacent values.
while j >= 0 and array[j] > item:
# Shift the value one position to the left
# and reposition j to point to the next element
array[j + 1] = array[j]
j = j - 1
print(j)
# When you finish shifting the elements, you can position
# item in its correct location
array[j + 1] = item
return array
a = [8, 2, 6, 5, 4]
# range(1, len(a))
start_time = time.time()
insertion_sort(a)
time.time() - start_time
### Merge Sort Algorithm (2 steps)
def merge(left, right):
result = []
index_left = index_right = 0
while len(result) < len(left) + len(right):
# sort the elements in the result array comparing
# pairs of values in the two arrays
if left[index_left] <= right[index_right]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1
# when end of either array is reached,remaining elements
# are added from the other array to the result and break
if index_right == len(right):
result += left[index_left:]
break
if index_left == len(left):
result += right[index_right:]
break
return result
def merge_sort(array):
if len(array) < 2:
return array
midpoint = len(array) // 2 # len(a) => 5 // 2 => 2
# splitting recursively the input into two equal halves,
# sorting each half and merging them together into the final result
left = merge_sort(array[:midpoint])
right = merge_sort(array[midpoint:])
return merge(left, right)
a = [8, 2, 6, 5, 4]
start_time = time.time()
merge_sort(a)
time.time() - start_time
### Quick Sort Algorithm
from random import randint
def quick_sort(array):
if len(array) < 2:
return array
low, same, high = [], [], [] # split in lists
# select pivot element randomly
pivot = array[randint(0, len(array) - 1)]
for item in array:
# assign elements to each list
if item < pivot:
low.append(item)
elif item == pivot:
same.append(item)
elif item > pivot:
high.append(item)
# combine lists recursively
return quick_sort(low) + same + quick_sort(high)
a = [8, 2, 6, 5, 4]
start_time = time.time()
quick_sort(a)
time.time() - start_time