Sorts on the AP Exam Insertion Sort.

Slides:



Advertisements
Similar presentations
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Advertisements

Computer Programming Lab(5).
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
AP Java Java’s version of Repeat until.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Permutations and Combinations Review Plus a little Dry Run, Sorting and Code.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Chapter 7 – Arrays and Array Lists
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 5 Ordered List.
Maha AlSaif Maryam AlQattan
Chapter 5: Control Structures II
Something about Java Introduction to Problem Solving and Programming 1.
Java Methods Making Subprograms.
Control Statement Examples
Array Review Selection Sort
Chapter 5 Ordered List.
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Java Language Basics.
Java Variables, Types, and Math Getting Started
Java Methods Making Subprograms.
Truth tables: Ways to organize results of Boolean expressions.
Computer Science 2 Review the Bubble Sort
AP Java Review If else.
Insertion Sort Quiz on Thursday.
More on Classes and Objects
Computer Science 2 Arrays of Records.
Selection Insertion and Merge
Computer Science 2 Getting an unknown # of …. Into an array.
Truth tables: Ways to organize results of Boolean expressions.
Java Methods Making Subprograms.
Insertion Sort.
class PrintOnetoTen { public static void main(String args[]) {
Computer Science Sorting.
Array Lists CSE 1310 – Introduction to Computers and Programming
Introduction to Java Brief history of Java Sample Java Program
Dry Run Practice Use Methods with an Insertion Sort
Truth tables: Ways to organize results of Boolean expressions.
Java Array Lists Day 2.
AP Java Review If else.
Sorting Develop a sorting algorithm
Java 1/31/2017 Back to Objects.
Array Review Selection Sort
Insertion Sort.
Random Numbers while loop
PROGRAMMING ASSIGNMENT I
Selection Sort Insertion Sort if time
Insertion and Shell Sorts
Presentation transcript:

Sorts on the AP Exam Insertion Sort

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

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 20 10 15 8 2 40 1

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

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

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.

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 email. Create an array of People rather than an array of String for the above program.

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.