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.

Slides:



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

CSE Lecture 3 – Algorithms I
Chapter 9: Searching, Sorting, and Algorithm Analysis
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS4413 Divide-and-Conquer
Data Structures and Algorithms
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CHAPTER 11 Sorting.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
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.
Chapter 16: Searching, Sorting, and the vector Type.
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.
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.
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
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.
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
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 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
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 Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Linear and Binary Search
Introduction to Search Algorithms
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Arrays
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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 typical searching algorithm over an array returns the array index where the target item is found, or –1 if it is not found We will examine two specific algorithms: Linear search Binary search

Linear Search Linear search is also called sequential search The approach of linear search: look at each item in turn, beginning with the first one, until either you find the target item or you reach the end of the array

Linear Search int Found = 0; for ( i=0 ; i < 100 ; i++) { if ( A[i] == target ) { cout<<“ Found it ! “; found = 1; break ; } if (found ==0) cout<<“ Looked all over, Not here “;

Linear Search An example: How many array elements are compared with the target value if the target is 3? Answer: 1 How many array elements are compared with the target value if the target is 2? Answer: 5 How many array elements are compared with the target value if the target is 8? Answer: 7 How many array elements are compared with the target value if the target is 5? Answer: 7

Binary Search If an array is sorted Check whether the middle element is equal to target. If so, it done. Otherwise, determine which half of array the target should be in Perform binary search again on subarray by comparing its middle element with target Repeatedly divide array in half, until desired item is found, or you have a subarray contains only one element not equal to target, in which case target is not found.

Binary Search Example: target value is 25 Array = Middle element = 17 < 25, 25 should be in second half Subarray = Middle element = 34 > 25, 25 should be in first half Subarray = Middle element = 21 < 25, 25 should be in second half Subarray = 25 Middle element = 25, done!

Binary Search Example: target value is 6 Array = Middle element = 17 > 6, 6 should be in first half Subarray = Middle element = 7 > 6, 6 should be in first half Subarray = Middle element = 3 < 6, 6 should be in second half Subarray = 4 Middle element = 4 != 6, failed!

Binary Search int low = 0; int high = 99; int found = 0; int mid ; while ((low <= high) && (found==0)) { mid = (low + high) / 2; if (target == A[mid]) { cout<<“ Found it “; found = 1 ; } else if (target < A[mid]) high = mid - 1; /* consider 1 st half of array */ else low = mid + 1; /* consider 2 nd half of array */ } if (found == 0 ) cout<<“ Definitely NOT here “;

Example: tracing binary search Iteration target = 7, # comparisons = low mid high 9401 midlowIteration target = 9, # comparisons = 1

Example: tracing binary search highmidlowIteration target = 21, # comparisons = highmidlowIteration target = 22, # comparisons = 4

Sorting Sorting is process of arranging list in particular order There are three algorithms for sorting a list of items Selection Sort Insertion Sort Bubble sort

Bubble Sort During first pass, compare first two items in the array, exchange them if they are out of order. Compare the second item with third one, and swap them if they are out of order. You proceed, comparing and exchanging items until you reach the end of the array. During the second pass, you perform the same actions of comparing and exchanging every two adjacent items, until you finish comparing the third last item with the second last. Repeat the same process for n-1 passes

Bubble Sort Example: FIRST PASS SECOND PASS THIRD PASS FOURTH PASS FIFTH PASS

Bubble Sort for (int pass = 1; pass < 100; pass++) { for (int j = 0; j < pass; j++) { /* bubble the larger number to the right */ if (A[j] > A[j+1]) { int temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; } /* end if */ } /* end for */

Bubble Sort with Flag Observe if no swapping is performed during a pass, then it implies list of items is sorted. we can improve efficiency of bubble sort algorithm by introducing flag that is initialized to 0 at beginning of every pass and is set to 1 when a swap occurs. At the end of each pass, check if flag is still 0. If so, no swap has occurred at this pass, implying that the list is sorted. Thus, the bubble sort algorithm can terminate right away

Bubble Sort with Flag int swap = 1 ; /* Nothing swapped yet */ for (int pass = 1; pass < 100 && swap == 1; pass++) { swap = 0; /* assume sorted */ for (int j = 0; j < pass; j++) { /* bubble the larger number to the right */ if ( A[j] > A[j+1]) { int temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; swap = 1; /* swap implies not yet sorted */ } /* end if */ } /* end for */

Selection Sort find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places

Selection Sort An example: original: smallest is 1: smallest is 2: smallest is 3: smallest is 6:

Swapping Swapping requires three assignment statements temp = first; first = second; second = temp;

Insertion Sort consider first item to be sorted sublist (one item) insert second item into sorted sublist, shifting the first item as needed insert third item into sorted sublist (of two items), repeat until all values are inserted into their proper positions

Insertion Sort An example: original: insert 9: insert 6: insert 1: insert 2:

The End