Skip to content

Commit 87ec5ff

Browse files
Added Insertion, Bubble, Shell and Merge Sort
1 parent fcd6e2b commit 87ec5ff

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

sortingAlgo.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def selection(A):
1616
A[i], A[position] = A[position], A[i]
1717
print("Sorted using Selection Sort: ", A)
1818

19+
###############################################################
20+
1921
def insertion(A):
2022
"""
2123
Algo name: Insertion Sort
@@ -37,18 +39,131 @@ def insertion(A):
3739

3840
print("Sorted using Insertion Sort: ", A)
3941

42+
###############################################################
43+
44+
def bubble(A):
45+
"""
46+
Algo name: Bubble Sort
47+
STABLE
48+
input:
49+
A -- Array
50+
51+
returns sorted array
52+
"""
53+
n = len(A)
54+
for p in range(n-1, 0, -1):
55+
for i in range(0, p):
56+
if A[i] > A[i + 1]:
57+
A[i], A[i+1] = A[i+1], A[i]
58+
print("Sorted using Bubble Sort: ", A)
59+
60+
###############################################################
61+
62+
def shell(A):
63+
"""
64+
Algo name: Shell Sort
65+
UNSTABLE
66+
input:
67+
A -- Array
68+
69+
returns sorted array
70+
"""
71+
n = len(A)
72+
gap = n//2
73+
while gap > 0:
74+
i = gap
75+
while i < n:
76+
temp = A[i]
77+
j = i - gap
78+
while j >= 0 and A[j] > temp:
79+
A[j+gap] = A[j]
80+
j = j - gap
81+
A[j+gap] = temp
82+
i = i + 1
83+
gap = gap // 2
84+
print("Sorted using Shell Sort: ", A)
85+
86+
###############################################################
87+
88+
def merge(A, l, m, r):
89+
"""
90+
Algo name: Merge
91+
input:
92+
A -- Array
93+
l -- first index position of the array
94+
m -- middle index postion of the array calculated
95+
using l and r
96+
r -- last index position of the array
97+
98+
returns Sorted partial array A
99+
"""
100+
101+
i = l
102+
j = m + 1
103+
k = l
104+
B = [0] * (r + 1)
105+
while i <= m and j <= r:
106+
if A[i] < A[j]:
107+
B[k] = A[i]
108+
i = i + 1
109+
else:
110+
B[k] = A[j]
111+
j = j + 1
112+
k = k + 1
113+
114+
while i <= m:
115+
B[k] = A[i]
116+
i = i + 1
117+
k = k + 1
118+
while j <= r:
119+
B[k] = A[j]
120+
j = j + 1
121+
k = k + 1
122+
for x in range(l, r + 1):
123+
A[x] = B[x]
124+
125+
126+
def mergesort(A, left, right):
127+
"""
128+
Algo name: Merge Sort
129+
UNSTABLE
130+
input:
131+
A -- Array
132+
left -- first index position of the array(0 in first call)
133+
right -- last index position of the array(len(A) - 1 in the first call)
134+
135+
"""
136+
if left < right:
137+
mid = (left + right) // 2
138+
mergesort(A, left, mid)
139+
mergesort(A, mid + 1, right)
140+
merge(A, left, mid, right)
141+
142+
143+
###############################################################
40144
def switch_case(choice):
41145
if choice == 1:
42146
selection(A),
43147
elif choice == 2:
44148
insertion(A)
149+
elif choice == 3:
150+
bubble(A)
151+
elif choice == 4:
152+
shell(A)
153+
elif choice == 5:
154+
mergesort(A, 0, len(A)-1)
155+
print("Sorted using Merge Sort: ", A)
45156

157+
###############################################################
46158

47159
A = list(map(int, input("Enter Array elements: ").split()))
48160

49161
print("Select Sorting Method")
50162
print("1. Selection Sort")
51163
print("2. Insertion Sort")
164+
print("3. Bubble Sort")
165+
print("4. Shell Sort")
166+
print("5. Merge Sort")
52167

53168
choice = int(input("Enter your choice: "))
54169
switch_case(choice)

0 commit comments

Comments
 (0)