Skip to content

Commit fcd6e2b

Browse files
Added 2 sorting methods
1 parent ac33a2c commit fcd6e2b

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

binarysearch.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import math
22

3-
43
def bs_itr(A, n, key):
54
low = 0
65
high = n - 1
@@ -42,4 +41,4 @@ def bs_recur(A, key, low = 0, high = len(A) - 1):
4241

4342
print("Recursive Binary Search output:", bs_recur(A, 10))
4443
print("Recursive Binary Search output:", bs_recur(A, 5))
45-
print("Recursive Binary Search output:", bs_recur(A, 2))
44+
print("Recursive Binary Search output:", bs_recur(A, 2))

sortingAlgo.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
def selection(A):
2+
"""
3+
Algo name: Selection Sort
4+
UNSTABLE
5+
input:
6+
A -- Array
7+
8+
returns sorted array
9+
"""
10+
n = len(A)
11+
for i in range(n-1):
12+
position = i
13+
for j in range(i + 1, n):
14+
if A[j] < A[position]:
15+
position = j
16+
A[i], A[position] = A[position], A[i]
17+
print("Sorted using Selection Sort: ", A)
18+
19+
def insertion(A):
20+
"""
21+
Algo name: Insertion Sort
22+
STABLE
23+
input:
24+
A -- Array
25+
26+
returns sorted array
27+
"""
28+
n = len(A)
29+
30+
for i in range(1, n):
31+
cvalue = A[i]
32+
position = i
33+
while position > 0 and A[position - 1] > cvalue:
34+
A[position] = A[position - 1]
35+
position = position - 1
36+
A[position] = cvalue
37+
38+
print("Sorted using Insertion Sort: ", A)
39+
40+
def switch_case(choice):
41+
if choice == 1:
42+
selection(A),
43+
elif choice == 2:
44+
insertion(A)
45+
46+
47+
A = list(map(int, input("Enter Array elements: ").split()))
48+
49+
print("Select Sorting Method")
50+
print("1. Selection Sort")
51+
print("2. Insertion Sort")
52+
53+
choice = int(input("Enter your choice: "))
54+
switch_case(choice)

0 commit comments

Comments
 (0)