forked from subhashissuara/SortingAlgoTimeComparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortingAlgos.py
119 lines (106 loc) · 3.5 KB
/
sortingAlgos.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
# --------------------------
# Author: Subhashis Suara
# Student ID: UCSE19012
# Sorting algorithms:
# Bubble Sort
# Selection Sort
# Insertion Sort
# Merge Sort
# Quick Sort
# --------------------------
import math
def bubbleSort(inputArray):
size = len(inputArray)
for i in range(size):
for j in range(size - i - 1):
if inputArray[j] > inputArray[j + 1]:
temp = inputArray[j]
inputArray[j] = inputArray[j + 1]
inputArray[j + 1] = temp
def selectionSort(inputArray):
size = len(inputArray)
for i in range(size):
minElemIndex = i
for j in range(i + 1, size):
if(inputArray[minElemIndex] > inputArray[j]):
minElemIndex = j
temp = inputArray[minElemIndex]
inputArray[minElemIndex] = inputArray[i]
inputArray[i] = temp
def insertionSort(inputArray):
size = len(inputArray)
for i in range(1, size):
key = inputArray[i]
j = i - 1
while j >= 0 and inputArray[j] > key:
inputArray[j + 1] = inputArray[j]
j -= 1
inputArray[j + 1] = key
def mergeSort(inputArray, p, r):
def combine(inputArray, p, q, r):
leftArraySize = q - p + 1
rightArraySize = r - q
leftArray = [None] * (leftArraySize + 1)
rightArray = [None] * (rightArraySize + 1)
for i in range(leftArraySize):
leftArray[i] = inputArray[p + i]
for j in range(rightArraySize):
rightArray[j] = inputArray[q + j + 1]
leftArray[leftArraySize] = math.inf
rightArray[rightArraySize] = math.inf
i = 0
j = 0
for k in range(p, r + 1):
if (leftArray[i] < rightArray[j]):
inputArray[k] = leftArray[i]
i += 1
else:
inputArray[k] = rightArray[j]
j += 1
if (p < r):
q = (p + r) // 2
mergeSort(inputArray, p, q)
mergeSort(inputArray, q + 1, r)
combine(inputArray, p, q, r)
# Alternate Method using slicing
# def mergeSort(inputArray):
# def combine(inputArray, leftArray, rightArray):
# sizeInputArray = len(inputArray)
# leftArray.append(math.inf)
# rightArray.append(math.inf)
# i = 0
# j = 0
# for k in range(sizeInputArray):
# if leftArray[i] < rightArray[j]:
# inputArray[k] = leftArray[i]
# i += 1
# else:
# inputArray[k] = rightArray[j]
# j += 1
# k += 1
# size = len(inputArray)
# if size > 1:
# midPoint = size // 2
# leftArray = inputArray[:midPoint]
# rightArray = inputArray[midPoint:]
# mergeSort(leftArray)
# mergeSort(rightArray)
# combine(inputArray, leftArray, rightArray)
def quickSort(inputArray, p, r):
def partition(inputArray, p, r):
pivotElem = inputArray[r]
i = p - 1
for j in range(p, r):
if(inputArray[j] < pivotElem):
i += 1
temp = inputArray[i]
inputArray[i] = inputArray[j]
inputArray[j] = temp
temp = inputArray[i + 1]
inputArray[i + 1] = inputArray[r]
inputArray[r] = temp
return i + 1
if(p < r):
partitionPoint = partition(inputArray, p, r)
quickSort(inputArray, p, partitionPoint - 1)
quickSort(inputArray, partitionPoint + 1, r)