Skip to content

Commit db727d1

Browse files
committed
Add Insertion Sort
1 parent e1bd6c2 commit db727d1

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

src/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Main {
77
public static void main(String[] args) {
88
int[] arr = {5, 3, 4, 2, 1};
99

10-
SortingService.sort(arr, SortingAlgorithms.SELECTION_SORT);
10+
SortingService.sort(arr, SortingAlgorithms.INSERTION_SORT);
1111

1212
System.out.println(Arrays.toString(arr));
1313
}

src/sort/SortingService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort;
22

33
import sort.bubble.BubbleSort;
4+
import sort.insertion.InsertionSort;
45
import sort.selection.SelectionSort;
56

67
public class SortingService {
@@ -16,6 +17,9 @@ public static void sort(int[] arr, SortingAlgorithms sortAlgorithms) {
1617
case SELECTION_SORT:
1718
SelectionSort.sort(arr);
1819
break;
20+
case INSERTION_SORT:
21+
InsertionSort.sort(arr);
22+
break;
1923
}
2024
}
2125
}

src/sort/bubble/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Starting from the beginning of the list, compare the next pair. Swap their posit
99
### Time Complexity
1010

1111
---
12-
1. Worst Case: O(n<sup>2</sup>). The worst case occurs when we want to sort a list in ascending order, but it is arranged in descending order.
13-
2. Best Case: O(n). The average case is when the list is arranged in a jumbled order.
14-
3. Average Case: O(n). The best case occurs when the list is already arranged in the desired order.
12+
1. **Worst Case: O(n<sup>2</sup>).** The worst case occurs when we want to sort a list in ascending order, but it is arranged in descending order.
13+
2. **Best Case: O(n).** The average case is when the list is arranged in a jumbled order.
14+
3. **Average Case: O(n).** The best case occurs when the list is already arranged in the desired order.
1515

1616
### Space Complexity
1717

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sort.insertion;
2+
3+
public class InsertionSort {
4+
public static void sort(int[] arr) {
5+
int n = arr.length;
6+
7+
// loop to select each element
8+
// first element is already sorted
9+
for (int i = 1; i < n; i++) {
10+
// element to be moved to its correct place
11+
int key = arr[i];
12+
// comparison starts from the left adjacent element of key till zero
13+
int j = i - 1;
14+
15+
// go backward until you find element > key
16+
// stop as soon as you find element < key
17+
while (j >= 0 && arr[j] > key) {
18+
// shift greater elements to the right
19+
arr[j + 1] = arr[j];
20+
j--;
21+
}
22+
23+
// index of smaller element than key + 1
24+
// is the correct place for key
25+
arr[j + 1] = key;
26+
}
27+
}
28+
}

src/sort/insertion/README.MD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Insertion Sort
2+
*Move element to its correct place to the left*
3+
4+
### How It Works
5+
![Insertion Sort](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
6+
7+
The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (red) is removed from the "not yet checked for order" input data and inserted in-place into the sorted list.
8+
9+
### Time Complexity
10+
11+
---
12+
1. **Worst Case Complexity: O(n<sup>2</sup>)**
13+
Suppose, an array is in ascending order, and you want to sort it in descending order. In this case, worst case complexity occurs.
14+
Each element has to be compared with each of the other elements so, for every nth element, (n-1) number of comparisons are made.
15+
Thus, the total number of comparisons = n*(n-1) ~ n2
16+
2. **Best Case Complexity: O(n)**
17+
When the array is already sorted, the outer loop runs for n number of times whereas the inner loop does not run at all. So, there are only n number of comparisons. Thus, complexity is linear.
18+
3. **Average Case Complexity: O(n<sup>2</sup>)**
19+
It occurs when the elements of an array are in jumbled order (neither ascending nor descending).
20+
21+
22+
### Space Complexity
23+
24+
---
25+
1. Space complexity is O(1) because an extra variable key is used.
26+
27+
### Properties
28+
1. Comparison Based
29+
2. Not Stable
30+
3. Inplace
31+
4. Used in practise for small arrays (TimSort & IntroSort)
32+
33+
### Insertion Sort Applications
34+
1. The array has a small number of elements
35+
2. There are only a few elements left to be sorted

src/sort/selection/README.MD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
### Time Complexity
1212

1313
---
14-
1. Worst Case Complexity: O(n<sup>2</sup>)
14+
1. **Worst Case Complexity: O(n<sup>2</sup>)**
1515
If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
16-
2. Best Case Complexity: O(n<sup>2</sup>)
16+
2. **Best Case Complexity: O(n<sup>2</sup>)**
1717
It occurs when the array is already sorted
18-
3. Average Case Complexity: O(n<sup>2</sup>)
18+
3. **Average Case Complexity: O(n<sup>2</sup>)**
1919
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
2020

2121

0 commit comments

Comments
 (0)