1 / 10

Sorting - CIS 1068 Program Design and Abstraction

Sorting - CIS 1068 Program Design and Abstraction. Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu. Table of Contents. Introduction to sorting in Java Types of things you can sort Exercise. Mechanics of Sorting in Java.

jasonk
Download Presentation

Sorting - CIS 1068 Program Design and Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: zhen.jiang@temple.edu

  2. Table of Contents • Introduction to sorting in Java • Types of things you can sort • Exercise

  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):

  5. temperatureArrayList: • After Collections.sort(temperatureArrayList):

  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<T> interface.

  7. The Collections.sort() method applies to ArrayLists, but ArrayLists have types. • E.g., ArrayList<Integer>, ArrayList<Object>, ArrayList<String []> • Likewise, Collections.sort() can sort: • ArrayList<T>, if T implements the Comparable<T> interface

  8. Exercise • Insertion sorting and its implementation • http://www.cis.temple.edu/~jiang/1068practice14.pdf • Partial insertion sorting and its implementation • 2nd prize?

  9. public static int SecondPrize (int [ ] x){int p1, p2;if (x.length<2)return-1;if(x[0]<x[1]) {p1=1; p2=0;}else {p1 = 0; p2=1;}for (int i = 2; i<x.length; i++){ int v = x[i]; if (v>x[p1]){ p2 = p1; p1 = i;} else if (v>x[p2]){ p2 = i;}}return p2; }

  10. Partial insertion sorting and its implementation • 3rd prize?

More Related