-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
79 lines (68 loc) · 2.15 KB
/
Copy pathInsertionSort.java
File metadata and controls
79 lines (68 loc) · 2.15 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
/*
(c) Copyright 2018, 2019 Phasmid Software
*/
package edu.neu.coe.info6205.sort.elementary;
import edu.neu.coe.info6205.sort.BaseHelper;
import edu.neu.coe.info6205.sort.Helper;
import edu.neu.coe.info6205.sort.SortWithHelper;
import edu.neu.coe.info6205.util.Config;
public class InsertionSort<X extends Comparable<X>> extends SortWithHelper<X> {
/**
* Constructor for any sub-classes to use.
*
* @param description the description.
* @param N the number of elements expected.
* @param config the configuration.
*/
protected InsertionSort(String description, int N, Config config) {
super(description, N, config);
}
/**
* Constructor for InsertionSort
*
* @param N the number elements we expect to sort.
* @param config the configuration.
*/
public InsertionSort(int N, Config config) {
this(DESCRIPTION, N, config);
}
public InsertionSort(Config config) {
this(new BaseHelper<>(DESCRIPTION, config));
}
/**
* Constructor for InsertionSort
*
* @param helper an explicit instance of Helper to be used.
*/
public InsertionSort(Helper<X> helper) {
super(helper);
}
public InsertionSort() {
this(BaseHelper.getHelper(InsertionSort.class));
}
/**
* Sort the sub-array xs:from:to using insertion sort.
*
* @param xs sort the array xs from "from" to "to".
* @param from the index of the first element to sort
* @param to the index of the first element not to sort
*/
public void sort(X[] xs, int from, int to) {
final Helper<X> helper = getHelper();
// FIXME
for (int m = from + 1; m < to; m++) {
for (int n = m; n > from; n--) {
if (helper.less(xs[n], xs[n - 1])) {
helper.swap(xs, n - 1, n);
} else {
break;
}
}
}
// END
}
public static final String DESCRIPTION = "Insertion sort";
public static <T extends Comparable<T>> void sort(T[] ts) {
new InsertionSort<T>().mutatingSort(ts);
}
}