© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Recitation 2 Tuesday, January 27, General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things.
Searching and Sorting Arrays
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
CS1101: Programming Methodology Aaron Tan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
CSC 211 Data Structures Lecture 13
© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
The Data Types and Data Structures
Parameter Lists & Command Line Arguments
Chapter 7 Part 1 Edited by JJ Shepherd
Data Conversion & Scanner Class
Creating Objects & String Class
Sorting Data are arranged according to their values.
CprE 185: Intro to Problem Solving (using C)
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Sorting October 20, 2006 ComS 207: Programming I (in Java)
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Sorting Data are arranged according to their values.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Homework #7 Hints For the first 2 problems:  Instead of entering he numbers you can use the random number generator. Matrix multiplication Other Questions?

© 2004 Pearson Addison-Wesley. All rights reserved Problems with Input Scanner scan = new Scanner(System.in); String line = scan.nextLine(); int num = Integer.parseInt(line); String line2= scan.nextLine();

© 2004 Pearson Addison-Wesley. All rights reserved HW Hints: Matrix Multiplication [

© 2004 Pearson Addison-Wesley. All rights reserved HW Hints: Use For example, if you don’t know how to perform matrix multiplication just Google it!

© 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

© 2004 Pearson Addison-Wesley. All rights reserved Arrays in Java Java represents 2D arrays as an array of arrays! In other words, a 2D integer array is really a 1D array of references to 1D integer arrays. The concept generalizes to N-dimensions

© 2004 Pearson Addison-Wesley. All rights reserved Anatomy of a 2D Array [

© 2004 Pearson Addison-Wesley. All rights reserved Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer

© 2004 Pearson Addison-Wesley. All rights reserved Example of a regular 2D array [ Note: In Java the first index should be 0 not 1!

© 2004 Pearson Addison-Wesley. All rights reserved Example of a Ragged Array [ Note: In Java the first index should be 0 not 1!

© 2004 Pearson Addison-Wesley. All rights reserved Other Stuff Arrays as parameters to methods

© 2004 Pearson Addison-Wesley. All rights reserved Find the minimum number in an array

© 2004 Pearson Addison-Wesley. All rights reserved Search [

© 2004 Pearson Addison-Wesley. All rights reserved Linear Search The most basic Very easy to implement The array DOESN’T have to be sorted All array elements must be visited if the search fails Could be very slow

© 2004 Pearson Addison-Wesley. All rights reserved Example: Successful Linear Search [

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Failed Linear Search

Searching Not in the Textbook

© 2004 Pearson Addison-Wesley. All rights reserved Java Example: Finding the index of a number in a sorted array of integers using linear search

© 2004 Pearson Addison-Wesley. All rights reserved

Example: LinearSearch_InSortedArray.java

© 2004 Pearson Addison-Wesley. All rights reserved Analysis If the list is unsorted we have to search all numbers before we declare that the target is not present in the array. Because the list is sorted we can stop as soon as we reach a number that is greater than our target Can we do even better?

© 2004 Pearson Addison-Wesley. All rights reserved Binary Search At each step it splits the remaining array elements into two groups Therefore, it is faster than the linear search Works only on an already SORTED array Thus, there is a performance penalty for sorting the array [

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Successful Binary Search

© 2004 Pearson Addison-Wesley. All rights reserved Example: BinarySearch.java

© 2004 Pearson Addison-Wesley. All rights reserved

Analysis of Searching Methods For an array of size n Sequential Search (Average-Case) n/2 Sequential Search (Worst-Case) n Binary Search (Average-Case) log(n)/2 Binary Search (Worst-Case) log(n)

© 2004 Pearson Addison-Wesley. All rights reserved THE END