Dry Run Practice Use Methods with an Insertion Sort

Slides:



Advertisements
Similar presentations
The Singleton Pattern II Recursive Linked Structures.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Programming with Recursion
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Building Java Programs Chapter 13 Searching reading: 13.3.
Applications of Arrays (Searching and Sorting) and Strings
Computer Science Searching & Sorting.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Output Programs These slides will present a variety of small programs. Each program has some type of array that was introduced in this chapter. Our concern.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
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.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Sequential (Linear) Binary Selection** Insertion** Merge.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Permutations and Combinations Review Plus a little Dry Run, Sorting and Code.
Recursion Powerful Tool
Programming with Recursion
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Sorts, CompareTo Method and Strings
Lecture 18: Nested Loops and Two-Dimensional Arrays
Sorting Mr. Jacobs.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Data Structures in Java with JUnit ©Rick Mercer
Sorting Algorithms: Selection, Insertion and Bubble
Programming with Recursion
Arrays … The Sequel Applications and Extensions
Array Review Selection Sort
Topic 14 Searching and Simple Sorts
Searching and Sorting Linear Search Binary Search ; Reading p
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Chapter 8 Slides from GaddisText
CSE 1342 Programming Concepts
Outline Late Binding Polymorphism via Inheritance
Arrays in Java What, why and how Copyright Curt Hill.
Sorts on the AP Exam Insertion Sort.
AP Java Warm-up Boolean Array.
Computer Science 2 Review the Bubble Sort
Java Lesson 36 Mr. Kalmes.
Selection Insertion and Merge
Computer Science 2 Getting an unknown # of …. Into an array.
Topic 14 Searching and Simple Sorts
Java Array Lists 2/13/2017.
Search,Sort,Recursion.
PowerPoint Presentation Authors of Exposure Java
PowerPoint Presentation Authors of Exposure Java
Arrays.
Building Java Programs
Sorting and Searching -- Introduction
Array Review Selection Sort
CSC 143 Java Searching.
Module 8 – Searching & Sorting Algorithms
CS100A Sections Dec Loop Invariant Review C Review and Example
Insertion Sort Array index Value Insertion sort.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Dry Run Practice Use Methods with an Insertion Sort AP Java 1/11/2018 Dry Run Practice Use Methods with an Insertion Sort

Learning Targets Be able to read programs with methods Be able to create a program that uses methods and the Insertion Sort.

Multiple Choice 1) CTA9 (none). Consider the following static method public static double getSomething(int val) { val = 2 * val; val = val + 2 * val; return val; } Which of the following could be used to replace the body of getSomething so that the modified version will return the same result as the original version for all values of the parameter val. A. return 2 * val; B. return 3 * val; C. return 4 * val; D. return 5 * val; E. return 6 * val; Answer = E

2) CTA10 (A1.15). Consider the following static method public static double getSomething(int val) { val = 2 + val; val = val + 3*val; return val; } Which of the following could be used to replace the body of getSomething so that the modified version will return the same result as the original version for all values of the parameter val. A. return 4*val + 2; B. return 4*val + 6; C. return 4*val + 8; D. return 7*val + 6; E. return 7*val + 8; Multiple Choice Answer = C

Note how an array is passed to a method. 3) public static void sort(String [] list) { for(int start = 0; start < list.length-1; start++) int index = start; for(int k = start + 1; k < list.length; k++) if(list[k].compareTo(list[index]) > 0) index = k; } String temp = list[start]; list[start] = list[index]; list[index] = temp;   Assume the String array words is initialized as shown below. word What will the array word look like after the third pass through the outer loop in the call sort(word)? Note how an array is passed to a method. Bill Joe Sue Ann Mel Zeb C. Zeb Sue Mel Ann Joe Bill  

Warm up #4 R10 (A3.8) Consider the following method. // precondition: val > 0 public int zoom(int val) { if(val >= 100) return 2 * val; else return zoom(2*val); } What will be returned by the call zoom(30)? A. 60 B. 120 C. 180 D. 240 e. Nothing will be returned because of an infinite recursion.

Warm-up #5 R12 (A3.33) Consider the following method. public void printSomething(String str) { if(str.length() <= 1) System.out.print(str); else printSomething(str.subString(1)); System.out.print(str.subString(0, 1)); } What will be printed as a result of the call printSomething("abcd")? A. a B. d C. aaaa D. abcd E. dcba

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, What if sorting Strings? a[in] = a[in-1]; // Slide: shift item right, in--; // go left one position } a[in] = dummy; // Back: insert marked item } // end for

Second Insertion Sort Program Main Body Get 9 scores Call methods Show results Scores in order Low to High Mean, median Push: Mode, standard deviation Push: Handle getting an unknown number (but less than 100) of values. Methods Insertion Sort Mean Median