File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
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 } ' )
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments