Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorts on the AP Exam Insertion Sort.

Similar presentations


Presentation on theme: "Sorts on the AP Exam Insertion Sort."— Presentation transcript:

1 Sorts on the AP Exam Insertion Sort

2 Learning Objectives Review the Selection Sort
Be able to describe the Insertion Sort: Speed, Stability, How it works Be able to read code for the Insertion Sort Be able to write a program that uses the Insertion Sort

3 Selection Sort Review In note books answer the following
Speed Stable ? How does it work When should you use a Selection Sort? Sort the following, showing the values after each pass until sorted. High Low

4 Insertion sort How it works Example Low High 8 6 7 3 15 1 5 Stability
Stability Speed Your turn High Low Dummy, slide, back Stable O(n2)

5 Insertion Sort // a is the name of the array
// nElems stores the number of elements being sorted // This example is for sorting an array of ints int in, out; for(out=1; out<nElems; out++) // out is dividing line { int dummy = a[out]; // dummy Need to modify for sorting different types in = out; // start shifts at out while(in>0 && a[in-1] >= dummy) // until one is smaller, a[in] = a[in-1]; // Slide: shift item right, in--; // go left one position } a[in] = dummy; // Back: insert marked item } // end for

6 First Program Check for Understanding
import java.util.Scanner; // program uses class Scanner public class InsertionSort { public static void main(String args[]) String[] names = new String[5]; String name; // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); for (int count = 0 ; count < names.length; count++) System.out.println("Please enter a name"); names[count] = input.next(); } for (int j=0; j<names.length; j++) System.out.println(names[j]); //Insert Sort Code here System.out.println("The names ..."); for (String nam: names) // For each String nam in the array names. System.out.println(nam); First Program Check for Understanding Enter this into BlueJ and test to make sure it is sorting.

7 Insertion Sort Program
Phoney book for an unknown # (<100) Names Write a program with a menu with the following choices. Add a name: Semantics of a while loop. Method or in main body. Show all names : Method Sort the names: Method using the Insertion Sort Find a name. The user will enter a name and the program will tell if the name is in your list or not. Method or main body. Pushes: Figure out how to store a name and phone number. This will make the program a bit more useful. Enhance the find part to handle some sort of misspelling. Extra letters, Case insensitive, … Describe what errors your method will be able to handle. Create a class People that will have fields for name, phone, and . Create an array of People rather than an array of String for the above program.

8 Shell for the Selection Sort program.
import java.util.Scanner; public class SortOfFun { public static void main(String [] args) } public static void insertionSort(String [] unsortedArray) public static void show(String [] arrayToShow) Shell for the Selection Sort program.


Download ppt "Sorts on the AP Exam Insertion Sort."

Similar presentations


Ads by Google