Searching and Sorting Arrays

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,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Searching Arrays Linear search Binary search small arrays
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
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.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
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.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 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.
Starting Out with C++, 3 rd Edition 1 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.
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Algorithms
Searching and Sorting Arrays
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 9: Searching, Sorting, and Algorithm Analysis
Sorting Algorithms.
Introduction to Search Algorithms
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 8 – Searching and Sorting Arrays
Search,Sort,Recursion.
Principles of Computing – UFCFA3-30-1
Searching and Sorting Arrays
Search,Sort,Recursion.
Searching and Sorting Arrays
Introduction to Sorting Algorithms
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Searching and Sorting Arrays Chapter 8 Searching and Sorting Arrays

Introduction to Search Algorithms A search algorithm is a method of locating a specific item of information in a larger collection of data ( array ). This section discusses two algorithms for searching the contents of an array: Linear Search Binary Search

The Linear Search This is a very simple algorithm. It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for and stops when that value is found or the end of the array is reached.

Linear Search Algorithm: set found to false; set position to –1; set index to 0 while index < number-of-elements and found is false if list[index] is equal to search-value found = true position = index end if add 1 to index end while return position Example: Program Q-1

Linear Search – Tradeoffs / Efficiency Benefits: Easy algorithm to understand Array can be in any order Disadvantages: Inefficient (slow) for array of N elements, examines N/2 elements on average for value in array, N elements for value not in array

Binary Search The binary search is much more efficient than the linear search. It requires the list to be in order. The algorithm starts searching with the middle element. If the item is less than the middle element, it starts over searching the first half of the list. If the item is greater than the middle element, the search starts over starting with the middle element in the second half of the list. It then continues halving the list until the item is found.

Binary Search - Example Array numlist2 contains: Searching for the value 11, binary search examines 11 and stops Searching for the value 7, binary search examines 11, 3, 5, and stops 2 3 5 11 17 23 29

Binary Search Example: Program Q-2 Example Input: Enter the Employee ID you wish to search for: 199 That ID is found at element 4 in the array.

Efficiency of the Binary Search Much more efficient than the linear search. The minimum number of comparisons that the binary search will perform is 1 The maximum number of comparisons that the binary search will perform is x, where: 2X > N where N is the number of elements in the array [ x=log2N comparisons where N = array size ]

Binary Search Example searching for a value of 10 check performed: if ( array[middle] == value) true -> done array[middle] < value -> f = m + 1 array[middle] > value -> l = m - 1 index 1 2 3 4 5 6 7 8 9 value 11 12 13 15 first last middle M+1 M-1 stop The search value 10 was not found.

Binary Search Example searching for a value of 13 index 1 2 3 4 5 6 7 8 9 value 11 12 13 15 first last middle The search value 13 was found at element 8

Binary Search Example searching for a value of 2 index 1 2 3 4 5 6 7 8 9 value 11 12 13 15 first last middle The search value 2 was found at element 0

Lab Questions? What’s about searching number 19 in the following list using Binary search? What’s about number 12? 34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0

Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here: Bubble sort Selection sort

Bubble Sort Concept: Compare 1st two elements If out of order, swap them to put them in order Move to next element, compare 2nd and 3rd elements, swap them if necessary. Continue until end of array. Pass through array again, swapping as necessary Repeat until pass made with no swaps passes / compares

The Bubble Sort An easy way to arrange data in ascending or descending order. Pseudocode [ sort in ascending order ] Do set swap flag to false Set count variable to 0 For count is set to each subscript in Array from 0 to the next-to-last subscript If array[count] is greater than array[count+1] swap them set swap flag to true End if End for While swap flag is true

Bubble Sort Example (pass 1) Array numlist3 contains: 17 23 5 11 compare values 17 and 23 – in correct order, so no exchange compare values 23 and 11 – not in correct order, so exchange them compare values 23 and 5 – not in correct order, so exchange them

Bubble Sort Example (pass 2) After first pass, array numlist3 contains: 17 5 11 23 compare values 17 and 5 – not in correct order, so exchange them compare values 17 and 23 – in correct order, so no exchange compare values 17 and 11 – not in correct order, so exchange them

Bubble Sort Example (pass 3) After second pass, array numlist3 contains: 5 11 17 23 compare values 5 and 11 – in correct order, so no exchange compare values 17 and 23 – in correct order, so no exchange compare values 11 and 17 – in correct order, so no exchange No exchanges, so array is in order

Bubble Sort Example: Program Q-4 Program Output: The unsorted values are: 7 2 3 8 9 1 The sorted values are: 1 2 3 7 8 9

Bubble Sort - Tradeoffs Benefit: Easy to understand and implement Disadvantage: Inefficient: slow for large arrays

The Selection Sort The bubble sort is inefficient for large arrays because items only move by one element at a time. The selection sort moves items immediately to their final position in the array so it makes fewer exchanges ( swaps )

Selection Sort Concept for sort in ascending order: Locate smallest element in array. Exchange it with element in position 0 Locate next smallest element in array. Exchange it with element in position 1. Continue until all elements are arranged in order

Selection Sort – pass 1 Array numlist contains: Smallest element is 2. Exchange 2 with element in 1st position in array: 11 2 29 3 2 11 29 3

Selection Sort – pass 2, 3 Next smallest element is 3. Exchange 3 with element in 2nd position in array: Next smallest element is 11. Exchange 11 with element in 3rd position in array: 2 3 29 11 2 3 11 29

Selection Sort Pseudocode: For Start is set to each subscript in Array from 0 through the next-to-last subscript Set Index variable to Start Set minIndex variable to Start Set minValue variable to array[Start] For Index is set to each subscript in Array from Start+1 through the last subscript If array[Index] is less than minValue Set minValue to array[Index] Set minIndex to Index End if Increment Index End For Set array[minIndex] to array[Start] Set array[Start] to minValue

Selection Sort Example Example: Program Q-5 Program Output: The unsorted values are 5 7 2 8 9 1 The sorted values are 1 2 5 7 8 9