Skip to content

Commit 04faa53

Browse files
committed
Insertion Sort
1 parent 50ff9ae commit 04faa53

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

DSA/B03_Insertion_Sort.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.akashdipmahapatra.DSA;
2+
3+
public class B03_Insertion_Sort {
4+
5+
public static void main(String[] args) {
6+
7+
int arr[] = {6, 5, 2, 8, 9, 7};
8+
9+
10+
11+
// Insertion Sort
12+
for(int i=1; i<arr.length; i++){
13+
14+
int key = arr[i];
15+
int j = i-1;
16+
17+
while(j>0 && arr[j]>key){
18+
arr[j+1] = arr[j];
19+
j--;
20+
}
21+
arr[j+1]= key;
22+
}
23+
24+
25+
26+
for(int num : arr){
27+
System.out.print(num+" ");
28+
}
29+
}
30+
}

DSA/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,8 @@ Recursive Binary Search uses extra memory for **function call stack** (because e
226226
```
227227

228228
- Selection Short
229+
230+
<img src="../img/Selection%20Short.png">
231+
232+
- Insertion Sort
233+
> It's actually not `Swapping` but actully `Shifting`. completely take out all values and put into the array again in right order.

img/Selection Short.png

325 KB
Loading

0 commit comments

Comments
 (0)