Skip to content

Commit 43ef6ef

Browse files
authored
Add files via upload
1 parent 9e3f52d commit 43ef6ef

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Sorting_Algorithms/Insertion_Sort.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Insertion Sort - Inplace and Stable
2+
arr = [2,4,1,5,0,65,-9]
3+
n = len(arr)
4+
print(f'Before Sorting: {arr}')
5+
for i in range(1,n):
6+
val, curr_idx = arr[i], i
7+
# Moving all the elements of sorted part to their appropriate position
8+
while curr_idx > 0 and arr[curr_idx - 1] > val:
9+
arr[curr_idx] = arr[curr_idx-1]
10+
curr_idx -= 1
11+
12+
arr[curr_idx] = val
13+
14+
15+
16+
print(f'After Sorting: {arr}')

Sorting_Algorithms/Selection_Sort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Selection Sort Algorithm - Inplace and Unstable
2+
arr = [2,4,1,6,7,9]
3+
n = len(arr)
4+
print(f'Before Sorting: {arr}')
5+
for i in range(n):
6+
idx = i
7+
for j in range(i+1, n):
8+
if arr[j] < arr[idx]:
9+
idx = j
10+
# Swapping the numbers
11+
arr[i], arr[idx] = arr[idx], arr[i]
12+
13+
print(f'After Sorting: {arr}')
14+

0 commit comments

Comments
 (0)