public class SortArray
1: import java.util.Arrays;
3: /**
4: * An array that can count how many assignment and comparison operations take
5: * place involving its elements while it's being sorted.
6: *
7: * @author Mark Young (A00000000)
8: * @param <T> base type of the array
9: */
10: public class SortArray<T extends Comparable<? super T>> {
12: // ---------- Instance Variables ---------- //
13: private T[] arr;
14: public final int length;
15: private int numAsgn;
16: private int numComp;
18: // --------- Constructors ---------- //
19: /**
20: * Create an array of the given size.
21: *
22: * @param size the required size of the array
23: */
24: @SuppressWarnings("unchecked")
25: public SortArray(int size) {
26: if (size < 0) {
27: throw new IllegalArgumentException("Negative array size");
28: }
29: length = size;
30: arr = (T[]) new Comparable[length];
31: reset();
32: }
34: /**
35: * Reset all counts to zero.
36: */
37: public final void reset() {
38: numAsgn = numComp = 0;
39: }
41: /**
42: * Get the value at the given position of this array.
43: *
44: * @param posn the position to read from
45: * @return the value read
46: */
47: public T get(int posn) {
48: T value = arr[posn];
49: ++numAsgn;
50: return value;
51: }
53: /**
54: * Compare two elements of the array.
55: *
56: * @param lower one position (not necessarily the lower one)
57: * @param higher the other position (not necessarily the higher one)
58: * @return how the element at {@code lower} compares to the element at
59: * {@code higher}
60: */
61: public int compare(int lower, int higher) {
62: ++numComp;
63: return arr[lower].compareTo(arr[higher]);
64: }
66: /**
67: * Compare one element of the array to an external value.
68: *
69: * @param posn the position doing to comparing
70: * @param value the value to compare to
71: * @return how the element at {@code posn} compares to {@code value}
72: */
73: public int compareTo(int posn, T value) {
74: ++numComp;
75: return arr[posn].compareTo(value);
76: }
78: /**
79: * Swap the values at the given positions of the array.
80: *
81: * @param lower one position (not necessarily the lower one)
82: * @param higher the other position (not necessarily the higher one)
83: */
84: public void swap(int lower, int higher) {
85: T temp = arr[lower];
86: ++numAsgn;
87: arr[lower] = arr[higher];
88: ++numAsgn;
89: arr[higher] = temp;
90: ++numAsgn;
91: }
93: /**
94: * Return how many assignments to positions in the array have occurred since
95: * its creation or its last reset.
96: *
97: * @return the number of assignments made into the array
98: */
99: public int getNumAsgn() {
100: return numAsgn;
101: }
103: /**
104: * Return how many comparisons have been made involving elements of this
105: * array since it creation or its last reset.
106: *
107: * @return the number of comparisons involving elements of this array
108: */
109: public int getNumComp() {
110: return numComp;
111: }
113: /**
114: * Set the value at a position in this array.
115: *
116: * @param posn the position to set
117: * @param value the value to set it to
118: */
119: public void set(int posn, T value) {
120: arr[posn] = value;
121: ++numAsgn;
122: }
124: /**
125: * Move a value from one position in the array to another. The item is
126: * copied to the new position, but not erased from the old position.
127: *
128: * @param dst the position whose value is to be replaced
129: * @param src the position providing the new value
130: */
131: public void move(int dst, int src) {
132: arr[dst] = arr[src];
133: ++numAsgn;
134: }
136: @Override
137: public String toString() {
138: return Arrays.toString(arr);
139: }
141: /**
142: * Create an array using the given values.
143: *
144: * @param <U> the base type of the new array
145: * @param items the items to be included in the array
146: * @return an array containing exactly those values in that order
147: */
148: @SafeVarargs
149: @SuppressWarnings("varargs")
150: public static <U extends Comparable<? super U>>
151: SortArray<U> containing(U... items) {
152: SortArray<U> result = new SortArray<>(items.length);
153: for (int i = 0; i < items.length; ++i) {
154: result.set(i, items[i]);
155: }
156: result.reset();
157: return result;
158: }
160: }