Sequential (Linear) Binary Selection** Insertion** Merge
Selection sort swaps the current element with the lowest element from the remaining elements in the list. Selection Sort does not swap each time it finds elements out of position. Selection sort makes a complete pass while searching for the next item to swap. At the end of a pass once the item is located, one swap is made.
original pass pass pass pass Array length is 5. Number of passes = 5 – 1 = 4
public void selectionSort( int[] ray ) { for(int i=0; i< ray.length-1; i++) { int min = i; //min = location of lowest value for(int j = i+1; j< ray.length; j++) { if(ray[j] < ray[min]) min = j; //find location of lowest value } if( min != i) { int temp = ray[min]; ray[min] = ray[i]; ray[i] = temp; //put lowest value in pos i }
Recall that a class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects We can use polymorphism to develop a generic sort for any set of Comparable objects The sorting method accepts as a parameter an array of Comparable objects That way, one method can be used to sort an array of People, or Books, or whatever
Selection sort is pretty effective for small lists, but pretty horrible if used on large lists Selection sort consists of two loops The outer loops run based on the number of items in the list The inner loop runs to find the items that need to be moved
The inner loop either locates the spot with the smallest value or the spot with the largest value (depending on whether you are sorting it in ascending or descending order) After the inner loop completes, a swap may occur if needed At most, selection sort will make one swap per pass A pass is one complete execution of the inner loop
The insertion sort first selects an item and moves items up or down based on the comparison to the selected item. The idea is to get the selected item in proper position by shifting items around in the list. This is analogous to the way some people pick up playing cards and order them in their hands.
original after pass after pass after pass after pass Blue signifies elements checked during the pass. They are sorted relative to each other. Highlighted element is the one inserted during the pass.
void insertionSort( int[] stuff) { for (int i=1; i< stuff.length; i++) { int val = stuff[i]; //item to insert int j=i; while(j>0 && val<stuff[j-1]) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }
void insertionSort( String[] stuff ) { for (int i=1; i< stuff.length; i++) { String val = stuff[i]; //item to insert int j=i; while(j>0 && val.compareTo(stuff[j-1])<0) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }
Insertion Sort is normally more efficient than a Selection Sort Insertion Sort continually sorts the left side of the list while gradually moving to the end. This type of sort is similar to how many people sort cards in their hands
public class Athlete implements Comparable { private String lastName, firstName; //constructor and other methods not listed public int compareTo(Object obj) { Athlete otherAthlete = (Athlete) obj; if (lastName.compareTo(otherAthlete.lastName) 0) return 1; return 0; }
ArrayList list; list=new ArrayList (); list.add(new Athlete(“Bob”,”Smith”)); list.add(new Athlete(“Tom”,”Jones”)); list.add(new Athlete(“Sue”,”Adams”)); list.add(new Athlete(“Joe”,”Bass”)); list.add(new Athlete(“Sara”,”Weiss”)); Collections.sort(list); for(Athlete athlete : list ) System.out.println(athlete); OUTPUT Sue Adams Joe Bass Tom Jones Bob Smith Sara Weiss Athlete object class needs to implement Comparable Have to redefine compareTo() method toString() method will make the print work as desired
Classwork Read over the “Notes – Comparable” handout and answer the Comprehension Questions –On a separate sheet or typed up Begin working on Lab 10.1 – Polymorphic Sorting –Use Listings 10.8 – from your textbook as a guide (pg 507 – 510) –Due Wednesday at midnight (will have tomorrow in class to work on it)