-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
126 lines (119 loc) · 5.47 KB
/
InsertionSort.java
File metadata and controls
126 lines (119 loc) · 5.47 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Comparator;
/**
* A simple sorting algorithm that builds the final sorted array (or list) one
* at a time. Iterates through a collection, consuming one input element each
* repetition and grows a sorted output list. At each iteration, it removes
* one element from the input data, finds the location it belongs within the
* sorted list, and inserts it there. It repeats until no input elements remain.
* Typically sorting is done in-place, by iterating up the array, growing the
* sorted list behind it. At each-array position it checks the value against the
* largest value in the sorted list. If larger it leaves the element in place
* and moves to the next. If smaller, it finds the correct position within the
* sorted list, shifts all the larger values up to make space, and inserts it
* into the correct position.
*
* Less efficient on large lists than more advanced sorts (heapsort, mergesort,
* quicksort). HOWEVER, the Straight Insertion Sort is best for small or very
* nearly sorted lists. In fact, it is one of the fastest algorithms for
* sorting very small arrays, even faster than quicksort. As good quicksort
* implementations use insertion sort for arrays smaller than a certain threshold,
* also when arising as subproblems. Exact threshold depends on machine and must
* be determined experimentally, but commonly is around ten.
*
* Advantages:
* - Simple Implementation
* - Efficient for small data sets
* - Adaptive: meaning it is efficient for data sets that are already substantially
* sorted; the time complexity is O(kn) where each element in the input
* is no more than k places away from its sorted position
* - Stable: Does not change the relative order of elements with equal keys
* - In-Place: Only requires a constant amount O(1) of additional memory space
* - Online: Can sort a list as it receives it
*
* Runtimes: Best case input of insertion sort is an array that is already sorted, so
* it has a linear running time. During each iteration, the first remaining element of
* the input is only compared with the right-most element of the sorted subsection of
* the array. Worst case input is an array sorted in reverse order, every iteration of
* the inner loop will scan and shift the entire sorted subsection of the array before
* inserting the next element giving it a quadratic running time.
*
* Best- O(n)
* Avg - O(n^2)
* Worst- O(n^2)
*/
public class InsertionSort <E> {
/**
* Insertion sort on an array of integers
* @param data
*/
public static void insertionSort(int[] data){
int len = data.length;
// 1. Mark First Element as sorted
for(int k = 1; k < len; k++){ // Start with Second element
int c = data[k]; // 2. Extract the element c
int i = k; // Save the correct index for c
while (i > 0 && data[i-1] > c){ // 3. If the data[i-1] is greater
data[i] = data[i-1]; // 4. Move data[i-1] to the right
i--; // 5. Repeat until data[i-1] is lesser
}
data[i] = c; // 6. Insert element here after loop
}
}
/**
* Insertion sort on an array of characters
* @param data
*/
public static void insertionSort(char[] data){
int len = data.length;
// 1. Mark First Element as sorted
for(int k = 1; k < len; k++){ // Start with Second Character
char c = data[k]; // 2. Extract the element c
int i = k; // Save the correct index for c
while (i > 0 && data[i-1] > c){ // 3. If the data[i-1] is greater
data[i] = data[i-1]; // 4. Move data[i-1] to the right
i--; // 5. Repeat until data[i-1] is lesser
}
data[i] = c; // 6. Insert element here after loop
}
}
Comparator<E> comp = (E x, E y) -> compare(x,y);
/**
* Compares Two Keys by natural ordering
* @param x First key to compare
* @param y Second key to compare
* @return -1 if x < y,
* 0 if x==y,
* 1 if x > y
*/
protected int compare(E x, E y){
return comp.compare(x,y);
}
/**
* Insertion sort on an array of generic type
* @param data
*/
public void insertionSort(E[] data){
int len = data.length;
// 1. Mark First Element as sorted
for(int k = 1; k < len; k++){ // Start with Second Character
E e = data[k]; // 2. Extract the element c
int i = k; // Save the correct index for c
while (i > 0 && comp.compare(e,data[i-1]) <= 0){ // 3. If the data[i-1] is greater
data[i] = data[i-1]; // 4. Move data[i-1] to the right
i--; // 5. Repeat until data[i-1] is lesser
}
data[i] = e; // 6. Insert element here after loop
}
}
// data[i-1] > e
// e < data[i-1]
// x < y
// -1 or 0 (if duplicates allowed
//
public static void main(String[] args) {
char[] a = {'C', 'E', 'B', 'D', 'A', 'I', 'J', 'L', 'K', 'H', 'G', 'F'};
System.out.println(java.util.Arrays.toString(a));
insertionSort(a);
System.out.println(java.util.Arrays.toString(a));
}
}