Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings H&K Chapter 9 Instructor – Gokcen Cilingir
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
C++ Plus Data Structures
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
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.
Chapter 8 ARRAYS Continued
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
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.
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
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.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Searching and Sorting Arrays
Searching and Sorting Arrays
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
(2-1) Data Structures & The Basics of a Linked List I
(2-1) Data Structures & The Basics of a Linked List I
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Arrays
Presentation transcript:

Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University

C. Hundhausen, A. O’Fallon2 Array Searching Lots of motivating problems  Find a name in the phone book  Find a student in the class list  Others?

C. Hundhausen, A. O’Fallon3 Solution strategy: search list sequentially until we find the target or until we’re sure that target is not in the list Get the value of target and the list of values Set the value of index to 0 Set the value of found to false While (index < n) and (found == false) if target = list[index] set found to true else set index to index + 1 Endwhile Note how we stop once we find the target. Sequential Search Algorithm

C. Hundhausen, A. O’Fallon4 Sequential search implementation with C int sequential_search( const int values[], int target, int n) { int index, found; index = 0; found = 0; // false //search until found or end of array while (index < n && !found) { if (values[index] == target) // We've found the target found = 1; else // Keep looking index++; } //Handle the result of the search if (found) // return index at which target was found return index; else // return NOT_FOUND flag return NOT_FOUND; // #defined as -1 } Indicates it’s a strictly input parameter

Sequential search on a sorted array If the input to sequential search algorithm was a sorted array (ascending order), we would change our loop as follows to save from execution time: while (index < n ) { if (values[index] == target) return index; // We've found the target else if (values[index] > target) return NOT_FOUND; //We’re sure target is not there else // Keep looking index++; } return NOT_FOUND; We can save much more from execution time by using binary search algorithm.

C. Hundhausen, A. O’Fallon6 Binary Search Algorithm Input: a list of n sorted values and a target value Output: True if target value exists in the list and location of target value, false otherwise Method: ◦ Set left to 0 and right to n-1 ◦ Set found to false ◦ While found is false and left is less than or equal to right  Set mid to midpoint between left and right  If target == item at mid then set found to true  If target < item at mid then set right to mid – 1  If target > item at mid then set to left to mid + 1 ◦ If found == true then print “Target found at location mid” ◦ Else print “Sorry, target value could not be found.”

Binary Search Algorithm(2) Idea is to narrow down the search space by half until either we find the target or we cannot narrow down the space any more which means target is not in the list In the beginning, our search space is the whole array During the search, we’re going to keep track of the narrowed search space (which will be a sub-array) by advancing the beginning and the end index of our “logical” search space.

Binary Search Visualization Check out this applet to visualize the discussed binary search algorithm.this applet

C. Hundhausen, A. O’Fallon9 Binary search implementation with C int binary_search( const int values[], int target, int num_values) { int found, left, right, mid; found = 0; // false left = 0; right = num_values; while (!found && left <= right) { mid = (left + right)/2; if (target == values[mid]) found = 1; // true else if (target < values[mid]) // target < values[mid] right = mid - 1; else // target > values[mid] left = mid + 1; } if (found) return mid; else return NOT_FOUND; // #defined as -1 }

C. Hundhausen, A. O’Fallon10 Array Sorting (1) Lots of motivating problems  Print out a class list in alphabetical order  Order finish times in a race to determine who placed  Make look-up easier (preparation for binary search)

C. Hundhausen, A. O’Fallon11 Selection Sort Algorithm Input: a list of numbers Output: a list of the same numbers in ascending (increasing) order Method: ◦ Set marker that divides “sorted” and “unsorted” sections of list to the beginning of the list ◦ While the unsorted section of the list is not empty  Call upon Find_Smallest helper function to find index of the smallest value in “unsorted” section of list  Swap smallest value in "unsorted section of list" with first value in “unsorted” section of list  Move "sorted/unsorted" marker to right one position

Selection Sort Visualization Check out this applet to visualize the discussed selection sort algorithm.this applet

C. Hundhausen, A. O’Fallon 13 Selection sort implementation with C void selection_sort(int values[], int num_values) { int marker, index_of_smallest, temp; for (marker = 0; marker < num_values - 1; marker++) { /* Find the index of the smallest element in unsorted list*/ index_of_smallest = find_smallest(values,marker,num_values-1); /* Swap the smallest value in the subarray i+1.. num_values - 1 with the value[i], thereby putting into place of the ith element. */ temp = values[marker]; values[marker] = values[index_of_smallest]; values[index_of_smallest] = temp; } int find_smallest(int values[], int low, int high) { int smallest_index, i; smallest_index = low; for (i = low + 1; i <= high; i++) if (values[i] < values[smallest_index]) smallest_index = i; return smallest_index; }

14 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.