Introduction to Programming

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

Visual C++ Programming: Concepts and Projects
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Chapter 8 ARRAYS Continued
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Arrays 2.
Recitation 13 Searching and Sorting.
Introduction to Programming
Algorithm design and Analysis
Introduction to Search Algorithms
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Review of Arrays and Pointers
Standard Version of Starting Out with C++, 4th Edition
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
C Arrays (2) (Chapter 6) ECET 264.
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Search,Sort,Recursion.
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Introduction to Programming Ms. Knudtzon Monday, Nov 15, C Period Week 11

Homework Check Abstract Lights Out Lab Homework 10 (Edward, Jesse) Grading (me) Week 11

The Incredibles Tuesday Class: Movie at 2:00 pm Talk about computer animation Movie at 2:00 pm Meet at Comp Lab at 1:25 Lunch? Box Lunches Chipotle? Bagels? Week 11

2D arrays Used to represent tables of values in rows and columns 1 2 3 1 2 3 4 5 6 7 8 On AB test only A test covers 1d arrays, AB test covers 2d arrays 1 2 Week 11

And remember: for loops and arrays go together! For a 2d array, we will need nested for loops: for(int row = 0; row < array.length; row++){ for(int col = 0; col < array[row].length; col++){ System.out.print(array[row][col] + “ “); } System.out.println(); Week 11

Example: DoubleArray.java Keeps track of rows of students and columns of grades Also has a second array that keeps track of the names that correspond to the rows of students Since arrays have to hold the same Data type in each element, we need the second array if we want to correspond the rows with actual student names. Week 11

Review of Pass-By Pass-by-Value Pass by reference When we pass an argument by value to a method, a copy of the argument’s value is made and passed to the called method Pass by reference When we pass by reference, the caller gives the called method the ability to access the caller’s data directly and possibly modify that data (it is not copied) Week 11

Trying it out… (Lab) Pass by value Pass by reference PassByExample.java Week 11

Special Computer Animation Lecture See Ms. K for the slides Tuesday, November 16 Special Computer Animation Lecture See Ms. K for the slides Week 11

Introduction to Programming Ms. Knudtzon Thursday, Nov 18, C Period Week 11

Problems on Homework 10 GradeBook homework Following specifications! Repeating code – SumArray did the work, AverageArray was supposed to call it Readable, easy to follow code Smallest and largest values in an array What should they be initialized to before you start searching? When do you print in a for loop/ when do you print when the loop has finished? Specifically for Summing elements or averaging elements Also for calculations, better not to repeat a calculation that isn’t needed (like computing average each time inside a loop) Week 11

Lights Out Lab What does “!” mean? It is used to take the opposite meaning in a boolean We’ve used != to check if something is not equal Can also be used to assign values In this lab, many of you wrote: if(lights[x] == true) { lights[x] = false; } But you could have just reversed the value without checking it: lights[x] = ! lights[x]; Week 11

Questions 1d arrays 2d arrays Pass by value Pass by reference Quiz Tomorrow  Week 11

Sorting Arrays Sorting data is important for all sorts of computer applications Bubble sort Let smaller values slowly “bubble” their way to the top (of the array), and let larger values sink to the bottom Technique: use nested for loops to go through the array several times Each time through, compare successive pairs – if pair is in decreasing order, swap the values; if not, leave them as is D&D Section 7.7 Week 11

Bubble Sort public void bubbleSort (int array[]){ //loop to control the number of passes for(int pass = 1; pass < array.length; pass++){ //loop to control the number of comparisons for(int e = 0; e < array.length -1; e++){ //compare elements & swap if necessary if(array[e] > array[e + 1] ) swap(array, e , e+1); } Fig 7.10 D&D Week 11

swap public void swap(int array2[],int first,int second){ int holder; //temp holding area for swap holder = array2[first]; array2[first] = array2 [second]; array2[second] = holder; } Fig 7.10 D&D Week 11

How does this work? Run BlueJ code with the debugger to step through the program – Can use object inspector to examine the array at different points in the code’s execution Note: the array could also be a variable with class scope and then you wouldn’t have to pass it as a parameter Week 11

Searching If information is stored in arrays, it might be necessary at some point to see if an array contains a certain key value Linear Search Binary Search Week 11

Linear Search Step through every element in the array to see if the key value is in the array Works well for small arrays or for unsorted arrays For large arrays, linear searching is inefficient Week 11

Linear Search Method private int linearSearch(int array[], int key){ for(int i = 0; i < array.length; i++){ if(array[i] == key){ return i; } return -1; Week 11

Linear Search in Action Welcome to linear search First we create an array: Please enter the number of elements in the array > 10 Please enter element 1 > 3 Please enter element 2 > 36 Please enter element 3 > 347 Please enter element 4 > 235 Please enter element 5 > 346 Please enter element 6 > 46 Please enter element 7 > 23 Please enter element 8 > 63 Please enter element 9 > 36 Please enter element 10 > 36 Please enter the key value you want to find > 46 Now Searching the array.... Value was found stored in position: 6 Week 11

Note on searches Typically it isn’t done all in one program like this. In general, you would have things stored and due to a user inquiry, you would want to search for it. These examples just show the basics of how these algorithms work. Week 11

Binary Search For sorted arrays, binary search is a higher-speed search technique Eliminates half the elements in the array after each comparison by looking at the middle elements’ value - if value matches key returns that elements’ index, otherwise it can reduce the problem to one half of the array – and so on and so forth Week 11

Binary Search Method public int binarySearch(int array[], int key){ int low = 0; int high = array.length - 1; int middle; //loop until low index reaches high while (low <= high){ middle = (low + high) /2; if(key == array[middle] ) { return middle; //return the found location! } else if(key < array[middle]){ high = middle - 1; else{ low = middle + 1; return -1; //key not found while looping Week 11

Binary Search in Action Welcome to binary search!! First we create an array: Please enter the number of elements in the array > 5 Please enter element 1 > 35 Please enter element 2 > 3467 Please enter element 3 > 47 Please enter element 4 > 25 Please enter element 5 > 457 Please enter the key value you want to find > 35 Information: Array is being sorted Press any key to continue > Information: Array has been sorted Press any key to start searching the array for the keyValue: 35 > **** Value was found stored in position: 2 **** Press any key to see the sorted array. > Element 1 is 25 Element 2 is 35 Element 3 is 47 Element 4 is 457 Element 5 is 3467 Week 11