forked from OneCodeMonkey/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion.java
More file actions
144 lines (124 loc) · 3.91 KB
/
Insertion.java
File metadata and controls
144 lines (124 loc) · 3.91 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Sorts a sequence of strings from standard input using insertion sort.
*
*/
import java.util.Comparator;
/**
* The `Insertion` class provides static methods for sorting an array using insertion sort.
* This implementation makes ~ 1/2 n^2 compares and exchanges in the worst case, so it is not suitable
* for sorting large arbitrary arrays.
* More precisely, the number of exchanges is exactly equal to the number of inversions.
* So, for example, it sorts a partially-sorted array in linear time.
*
*/
public class Insertion {
// THis class should not be instantiated.
private Insertion() {}
// Rearranges the array in ascending order, using the natural order.
public static void sort(Comparable[] e) {
int n = e.length;
for(int i = 1; i < n; i++) {
for(int j = i; j > 0 && less(a[j], a[j - 1]); j--) {
exchange(a, j, j - 1);
}
assert isSorted(a, 0, i);
}
assert isSorted(a);
}
// Rearrange the subarray a[low, high] in ascending order, using the natural order.
public static void sort(Comparable[] a, int low, int high) {
for(int i = low + 1; i < high; i++) {
for(int j = i; j > low && less(a[j], a[j - 1]); j--) {
exchange(a, j, j - 1);
}
}
assert isSorted(a, low, high);
}
// Rearrange the array in ascending order with a comparator
public static void sort(Object[] a, Comparable comparator) {
int n = a.length;
for(int i = 1; i < n; i++) {
for(int j = i + 1; j > 0 && less(a[j], a[j - 1], comparator); j--) {
exchange(a, j, j - 1);
}
assert isSorted(a, 0, i, comparator);
}
assert isSorted(a, comparator);
}
// Rearranges the subarray a[low, high] in ascending order with a comparator
public static void sort(Object[] a, int low, int high, Comparable comparator) {
for(int i = low + 1; i < high; i++) {
for(int j = i + 1; j > low && less(a[j], a[j - 1], comparator); j--)
exchange(a, j, j - 1);
}
assert isSorted(a, low, high, comparator);
}
// return a permutation that gives the elements in a[] in ascending order
// do not change the original array a[]
// Returns a permutation that gives the elements in the array in ascending order.
public static int[] indexSort(Comparable[] a) {
int n = a.length;
int[] index = new int[n];
for(int i = 0; i < n; i++) {
index[i] = i;
}
for(int i = 1; i < n; i++)
for(int j = i; j > 0 && less(a[index[j]], a[index[j - 1]]); j--)
exchange(index, j, j - 1);
return index;
}
// Helper sorting functions.
// is v < w?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
// is v < w?
private static boolean less(Object v, Object w, Comparator comparator) {
return comparator.compare(v, w) < 0;
}
// exchange a[i] and a[j]
private static void exchange(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
}
// exchange a[i] and a[j] (for indirect sort)
private static void exchange(int[] a, int i, int j) {
int swap = a[i];
a[i] = a[j];
a[j] = swap;
}
// check if array is sorted (for debug)
private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length);
}
// is the array a[low, high] sorted ?
private static boolean isSorted(Comparable[] a, int low, int high) {
for(int i = low + 1; i < high; i++)
if(less(a[i], a[i - 1]))
return false;
return true;
}
private static boolean isSorted(Object[] a, Comparator comparator) {
return isSorted(a, 0, a.length, comparator);
}
// is the array a[low, high] sorted ?
private static boolean isSorted(Object[] a, int low, int high, Comparable comparator) {
for(int i = low + 1; i < high; i++) {
if(less(a[i], a[i - 1], comparator))
return false;
}
return true;
}
// print the array to std::output
private static void show|(Comparable[] a) {
for(int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
// test
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Insertion.sort(a);
show(a);
}
}