-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInsertionSort.java
54 lines (38 loc) · 1.19 KB
/
InsertionSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// http://www.geeksforgeeks.org/insertion-sort/
/*
Time Complexity - O(n^2)
Space Complexity - O(1)
Boundary Cases: Insertion sort takes maximum time to sort if elements are sorted in reverse order.
And it takes minimum time (Order of n) when elements are already sorted.
Uses: Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.
Algorithmic Paradigm: Incremental Approach
Sorting In Place: Yes
Stable: Yes
Online: Yes
*/
class InsertionSort {
static void insertionSort(int[] arr) {
for(int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
public static void main(String[] args) {
System.out.println("Insertion Sort");
int[] arr = new int[] {5, 2, 4, 3, 1};
int n = arr.length;
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
insertionSort(arr);
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}