Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequential (Linear) Binary Selection** Insertion** Merge.

Similar presentations


Presentation on theme: "Sequential (Linear) Binary Selection** Insertion** Merge."— Presentation transcript:

1

2

3 Sequential (Linear) Binary Selection** Insertion** Merge

4

5 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.

6 original92851 0 1 2 3 4 pass 112859 pass 212859 pass 312589 pass 412589 Array length is 5. Number of passes = 5 – 1 = 4

7 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 }

8 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

9 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

10 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

11

12 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.

13 original92851 0 1 2 3 4 after pass 115892 after pass 2 15982 after pass 319852 after pass 498521 Blue signifies elements checked during the pass. They are sorted relative to each other. Highlighted element is the one inserted during the pass.

14 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; }

15 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; }

16 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

17 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; }

18 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

19 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 – 10.10 from your textbook as a guide (pg 507 – 510) –Due Wednesday at midnight (will have tomorrow in class to work on it)


Download ppt "Sequential (Linear) Binary Selection** Insertion** Merge."

Similar presentations


Ads by Google