Download presentation
Presentation is loading. Please wait.
Published byEustace Gibbs Modified over 9 years ago
1
Sorting - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu 13/14/2016
2
Table of Contents Introduction to sorting in Java Types of things you can sort Exercise 23/14/2016
3
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();
4
temperatureArray : After Arrays.sort(temperatureArray) : 51.733.921.662.159.044.5 21.633.944.551.759.062.1
5
temperatureArrayList : After Collections.sort(temperatureArrayList) : 51.733.921.662.159.044.5 21.633.944.551.759.062.1
6
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.
7
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
8
Exercise 83/14/2016 Insertion sorting and its implementation http://www.cis.temple.edu/~jiang/1068practice14.pdf Partial insertion sorting and its implementation 2 nd prize?
9
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; }
10
103/14/2016 Partial insertion sorting and its implementation 3 rd prize?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.