Skip to content

Commit 04a24fe

Browse files
authored
Merge pull request deutranium#92 from AangTheLast/Insertion-Sort
Added Readme File For Insertion Sort
2 parents d9e0b05 + 7e45d40 commit 04a24fe

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

sortingAlgo/insertionSort/Readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# Insertion Sort
3+
4+
- Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each iteration.
5+
- Time Complexity:
6+
- Worst Case Complexity:O(n2)
7+
- Best Case Complexity:O(1)
8+
- Average Case Complexity:O(n2)
9+
- Worst Case Space Complexity:O(1)
10+
11+
### Logic
12+
13+
1. The first element in the array is assumed to be sorted. Take the second element and store it separately in `key`.Compare `key` with the first element. If the first element is greater than `key`, then key is placed in front of the first element..
14+
2. Now, the first two elements are sorted. Take the third element and compare it with the elements on the left of it. Placed it just behind the element smaller than it. If there is no element smaller than it, then place it at the beginning of the array.
15+
3. Similarly, place every unsorted element at its correct position.
16+
### Pseudo Code:
17+
18+
for j <- 2 to length [_A_]
19+
20+
do key <- A [_j_]
21+
22+
Insert A [j] into the sorted sequence A[1..j-1]
23+
24+
i<-j- 1
25+
26+
while i >0 and A[i] >key
27+
28+
do A[i+1]<-A[i]
29+
30+
i <-i-1
31+
32+
A[i + 1] <-key
33+
34+
### Instruction for Running code:
35+
36+
37+
- Cpp
38+
```
39+
g++ InsertionSort.cpp
40+
./a.out
41+
```
42+
- Python
43+
```
44+
python3 InsertionSort.py
45+
```

0 commit comments

Comments
 (0)