Array Review Selection Sort

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Mock test review Revision of Activity Diagrams for Loops,
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Computer Programming Lab(5).
Lecture 4 Loops.
Chapter 5 Loops.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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:
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
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.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Java Methods Making Subprograms.
Chapter 6 More Conditionals and Loops
Building Java Programs Chapter 7
Java Enter your code from FRQ to Shell
Describing algorithms in pseudo code
Sorting Algorithms IT12112 Lecture 07.
Java Fix a program that has if Online time for Monday’s Program
Conditions and Ifs BIS1523 – Lecture 8.
Selection sort Given an array of length n,
Building Java Programs
Java Methods Making Subprograms.
Truth tables: Ways to organize results of Boolean expressions.
Sorts on the AP Exam Insertion Sort.
Computer Science 2 Review the Bubble Sort
Building Java Programs
AP Java Review If else.
Selection Insertion and Merge
Computer Science 2 Getting an unknown # of …. Into an array.
Truth tables: Ways to organize results of Boolean expressions.
Java Fix a program that has if Online time for Monday’s Program
python.reset() Also moving to a more reasonable room (CSE 403)
int [] scores = new int [10];
Computer Science Sorting.
Computer Science Sorting Pre-Test Discussion/program
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Dry Run Practice Use Methods with an Insertion Sort
Truth tables: Ways to organize results of Boolean expressions.
AP Java Review If else.
Building Java Programs
Simple Sorting Algorithms
Sorting Develop a sorting algorithm
Java 1/31/2017 Back to Objects.
Building Java Programs
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
Array Review Selection Sort
Simple Sorting Algorithms
Random Numbers while loop
Building Java Programs
How do you do the following?
Selection Sort Insertion Sort if time
Presentation transcript:

Array Review Selection Sort Get out your notes. Array Review Selection Sort

Learning Objectives Be able to dry run programs that use arrays Understand and be able to implement a Selection Sort.

Dry run 1 int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( int index= 0 ; index < 5 ; index++ ) System.out.print( egArray[ index ] + " " );

Dry Run 2 int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( int index= 0 ; index < egArray.length ; index++ ) System.out.print( egArray[ index ] + " " );

Dry Run 3 int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( int index= 0 ; index < egArray.length ; index = index + 2 ) System.out.print( egArray[ index ] + " " );

#4) Modify the code int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; Fill in the blanks of the following code fragment so that the elements of the array are printed  in reverse order, starting with the last element. int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; for ( int index= ________ ; _____________ ; ______________ ) System.out.print( egArray[ index ] + " " )

Dry Run 5 int[] array = { 1, 4, 3, 6, 8, 2, 5}; int what = array[0]; // scan the array for ( int index=0; index < array.length; index++ ) { if ( array[ index ] > what ) what = array[ index ]; } System.out.println( what );

Dry Run 6 int[] array = { 1, 4, 3, 6, 8, 2, 5}; int what = array[0]; // scan the array for ( int index=0; index < array.length; index++ ) { if ( array[ index ] < what ) what = array[ index ]; } System.out.println( what );

Dry Run 7 int[] array = { 1, 4, 3, 6 }; int what = 0; // scan the array for ( int index=0; index < array.length; index++ ) { what = what + array[ index ] ; } System.out.println( what );

8) Fill in the blank in the following code fragment so that each element of the array is assigned twice the value of its index. int[] array = new int[10]; // scan the array for ( int index=0; index < array.length; index++ ) { _______________________ }

Dry Run 9 int [] list = { 10, 11, 4, 8, 2 } ; for(int start = 1; start < list.length; start++) { int temp = list[start]; int k = start; while(k > 0 && list[k-1] > temp) list[k] = list[k-1]; k--; } list[k] = temp; for (int s:list) System.out.print(s +" "); System.out.println();

Dry Run 10 public static void main (String [] args) { int [] a = { 10, 11, 4, 8, 2 } ; int out, in, min; int dummy; for (out=0; out<a.length-1; out++) min = out; for(in=out+1; in<a.length; in++) if(a[in] < a[min] ) min = in; dummy = a[out]; a[out] = a[min]; a[min] = dummy; for (int s:a) System.out.print(s +" "); System.out.println(); } // end for(out) }

Learning Objectives Review reading a nested loop program. Understand how the selection and insertion sorts work Write a program that uses the selection and insertion sorts.

What did the previous Code do?

Selection sort How it works Example Low High 2 6 8 3 15 1 7 Stability 2 6 8 3 15 1 7 Stability Speed Your turn High Low 8 2 5 3 9 4 6 1 7 Check, mark, switch Not stable O(n2)

Selection Sort //a is the array name, // nElems = the number of elements being sorted int out, in, min; int dummy; // If sorting an array of ints for(out=0; out<nElems-1; out++) // outer loop { min = out; // minimum for(in=out+1; in<nElems; in++) // inner loop if(a[in] < a[min] ) // Check if min greater, min = in; // Mark, we have a new min dummy = a[out]; //Switch a[out] = a[min]; a[min] = dummy; } // end for(out)

First Program Check for Understanding import java.util.Scanner; // program uses class Scanner public class SelectionSort { 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 temp: names) // For each String temp in the array names. System.out.println(temp); First Program Check for Understanding Enter this into BlueJ and test to make sure it is sorting.

Selection Sort Program Input 11 scores in main body Output: Numbers in sorted order (Low to High): Method Mean (average): Method Median (Middle number after sorting): Method Push: Find the mode: The most often occurring value. Multiple modes? No mode? Let the user tell you how many scores prior to entering the scores Add a method that will return an array of the 5 highest scores. Add a method that will return the modified average. If the pre-sorted scores are in increasing order (continual improvement), it will return the average of the top 3 scores, otherwise with will return the entire average.

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

APCS Test Corrections What do I do to make corrections? Use your brain and, if necessary, chapter notes/text/notebook to find the correct answer. Write the correct answer. Write why you consider your answer to be correct, by supporting your answer Write why you got the answer wrong. This requires reflection on how you interpreted the question/answer and why you believe your misinterpreted it.

What will I get for making corrections? You will get 1/2 point added to your test score for each correct, complete and convincing correction. Other benefits include reviewing information, which confused you, increasing your understanding of Computer Science, and improving your multiple choice test taking skills. (For example, if you receive a 35/55 and you correct 10 incorrect answers, you have the possibility of receiving a 40/55 on your test.)

How long do I have to make corrections? You have one week after the exam is returned. Do I have to make corrections? No. You will keep your initial test score

What should corrections look like? Sample Question: Colonial American taverns were all of the following except: frequented mainly by the lower classes another cradle of democracy hotbeds of agitation for the Revolutionary movement important in crystallizing public opinion places providing amusement Question #1: _A_is the correct answer. I put D because I didn’t know the term “crystallizing.” I also didn’t understand “public opinion” so I thought this must be the “exception” answer. I did not pick the correct answer because I thought only lower classes hung out in taverns back then. This time, when I looked at all the choices, I realized b, c and d are about the Revolution so the most likely choices are either a or e. When I looked up information on the role of taverns, I found out all kinds of people hung out in taverns and people played cards and sang so they had amusement.

Will I get credit for every correction I make? No. Simply copying or paraphrasing the answer will not earn you any credit. You must show reasoning or reflection. Here is an example of how not to do it: Questions #1: The answer should be _A_. The “except” answer is “frequented mainly by the lower classes” because the other answers are correct.