-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.java
51 lines (42 loc) · 1.3 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
package sorting.iterative;
import utils.constants.Test;
import java.util.Arrays;
/*
Insertion sort: A simple sorting algorithm that builds the final sorted list one item at a time by repeatedly
inserting the next item into the correct position in the already sorted portion of the list (sorting playing cards).
*/
/*
method: incremental
in place: yes
stable: yes
comparison: yes
*/
/*
Note
> is an efficient algorithm for sorting a small number of elements
*/
/*
Time Complexity
BC: O(n) V is sorted (never enters the while loop)
WC: O(n^2) V is sorted in reverse (while loop exits only when i index reaches -1)
MC: O(n^2)
*/
public class InsertionSort { // implements Sorting
public static <T extends Comparable<T>> T[] sort(T[] V) {
T[] A = V.clone(); // return a copy
for (int j = 1; j < A.length; j++) {
T k = A[j];
// Insert A[j] into the sorted subarray A[0:j-1].
int i = j - 1;
while (i >= 0 && k.compareTo(A[i]) < 0) {
A[i + 1] = A[i];
i--;
}
A[i + 1] = k;
}
return (A);
}
public static void main(String[] args) {
System.out.println(Arrays.toString(InsertionSort.sort(Test.V)));
}
}