Sorting - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 13/14/2016
Table of Contents Introduction to sorting in Java Types of things you can sort Exercise 23/14/2016
Mechanics of Sorting in Java Java has built-in ways to sort arrays and ArrayLists: Arrays.sort(myArray); Collections.sort(myArrayList); Note that these are static methods in the Arrays and Collections classes, NOT instance methods! NOT: myArray.sort(); NOT: myArrayList.sort();
temperatureArray : After Arrays.sort(temperatureArray) :
temperatureArrayList : After Collections.sort(temperatureArrayList) :
Types of things you can sort The Arrays.sort() method applies to arrays, but arrays have types. E.g., int[], double[], Object[], Point[] Arrays.sort() can sort: any primitive array type any Object array type that implements the Comparable interface.
The Collections.sort() method applies to ArrayLists, but ArrayLists have types. E.g., ArrayList, ArrayList, ArrayList Likewise, Collections.sort() can sort: ArrayList, if T implements the Comparable interface
Exercise 83/14/2016 Insertion sorting and its implementation Partial insertion sorting and its implementation 2 nd prize?
3/14/20169 public static int SecondPrize (int [ ] x){ int p1, p2; if (x.length x[p1]){ p2 = p1; p1 = i;} else if (v>x[p2]){ p2 = i;} } return p2; }
103/14/2016 Partial insertion sorting and its implementation 3 rd prize?