// Dan Grossman // CSE142, Spring 2009 // A class with two static methods that sort arrays of Points // in order of their distance to the origin. // One uses the insertion sort algorithm; the other uses the // selection sort algorithm. main provides several tests. // (The point of this example is to show the offensive redundancy // with SortIntegerArray. Alas, to fix this well requires // techniques beyond 142 -- there's always more to learn!) // Notice the _code_ is different but the algorithm is really the same. import java.util.*; public class SortPointArray { // (external behavior: the purpose of a method comment) // return a new array that has the same contents as the argument, but // in sorted order. Does not change the argument (which is important // due to references). public static Point[] sort(Point[] unsorted) { // (internal algorithm: not visible to callers) // uses the "insertion sort" algorithm Point[] sorted = new Point[unsorted.length]; // for each element in the argument... // after i iterations, we are using the first i positions // in sorted for(int i=0; i < unsorted.length; i++) { int j = 0; // find where unsorted[i] should go while(j < i && unsorted[i].distanceToOrigin() > sorted[j].distanceToOrigin()) { j++; } // move everybody over to make room // tricky: important to start "on the right" // note: works even if 0 elements need to shift for(int k=i; k > j; k--) { sorted[k] = sorted[k-1]; } sorted[j] = unsorted[i]; } return sorted; } // external behavior ALMOST the same as sort to show there are // different sorting algorthms (resolves ties differently!) // note: could make exactly the same by changing one < to <= public static Point[] anotherSort(Point[] unsorted) { // (internal algorithm: not visible to callers) // uses the "selection sort" algorithm Point[] sorted = new Point[unsorted.length]; boolean[] used = new boolean[unsorted.length]; // initialized to false implicitly // repeatedly copy over the smallest element that // hasn't been moved over yet for(int i=0; i < unsorted.length; i++) { // find first not-moved-over, put its _index_ in j int j = 0; while(used[j]) { j++; } // now find smallest not-moved over and put its index in j for(int k=j+1; k < unsorted.length; k++) { if(!used[k] && unsorted[k].distanceToOrigin() < unsorted[j].distanceToOrigin()) { j = k; } } // move it over and mark it used sorted[i] = unsorted[j]; used[j] = true; } return sorted; } public static void testSort(Point[] arr) { System.out.println("initial array:\t" + Arrays.toString(arr)); System.out.println("sort result:\t" + Arrays.toString(sort(arr))); System.out.println("another result:\t" + Arrays.toString(anotherSort(arr))); System.out.println(); } public static void main(String[] args) { Point arr1[] = {new Point(0,1), new Point(2,0), new Point(0,4), new Point(-3,0)}; Point arr2[] = {new Point(0,0), new Point(1,1), new Point(-1,-1), new Point(2,2)}; Point arr3[] = {}; Point arr4[] = {new Point(9,9)}; Point arr5[] = {new Point(3,3), new Point(3,4), new Point(4,2), new Point(3,4)}; testSort(arr1); testSort(arr2); testSort(arr3); testSort(arr4); testSort(arr5); } }