CSC 211 Data Structures Lecture 13

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Introduction to Algorithms Quicksort
CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS4413 Divide-and-Conquer
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Efficiency of Algorithms
CS107 Introduction to Computer Science
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Algorithm Efficiency and Sorting
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Elementary Data Structures and Algorithms
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
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.
CSC 211 Data Structures Lecture 12
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Lecture 12. Searching Algorithms and its analysis 1.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
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.
Data Structure Introduction.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Arrays
Data Structures I (CPCS-204)
Introduction to Search Algorithms
Linear and Binary Search
Searching and Sorting Arrays
Searching and Sorting Arrays
Presentation transcript:

CSC 211 Data Structures Lecture 13 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

Last Lecture Summary Cursor-based Implementation of List Search operation Concepts and Definitions Linear (Sequential) Search Implementation of Linear search Complexity of Linear Search 2

Objectives Overview Binary Search Implementation of Binary Search Concept Algorithm Implementation of Binary Search Complexity of Binary Search Comparison of Linear and Binary Search Searching Unordered Linked List Searching Ordered Linked List

Binary Search A linear search is not efficient because on the average it needs to search half a list to find an item If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy A binary search is much faster than a linear search, but only works on an ordered list!

Binary Search Gets its name because the algorithm continually divides the list into two parts. Uses a "divide and conquer" technique to search the list. It starts with the middle element in a list. if that is it, the search is done. If the middle item is greater than the wanted item, throw out the last half of the list and search the first half. Otherwise, throw out the first half of the list and search the last half of the list.

Binary Search - Concept Take a sorted array A to find an element x First x is compared with middle element if they are equal search is successful, Otherwise if the two are not equal narrows the either to the lower sub array or upper sub array. The search continues by repeating same process over and over on successively smaller sub arrays. Process terminates either when a match occurs or when search is narrowed down to a sub array which contains no elements.

How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast ?

Binary Search - Example Suppose we have the following list of length 12. We want to find the item with the value of 75. We start with the whole list and find the middle item The middle item, in list[5] is equal to 39. Since 39 < 75, we restrict our search to last part of the list, which is items list[6] - list[11]

Binary Search - Example This process is repeated on this new list of length 6. The formula for finding the middle item is: Where initially, first = 0, and last = length - 1.

Binary Search Tracing -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 mid Search for target = 4. Step 1: Indices first= 0, last= 8, mid = (0+8)/2 = 4. -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 mid

Binary Search Tracing mid -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 Search for target = 4. Step 2: Indices first= 0, last= 3, mid = (0+3)/2 = 1 -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 mid Since target = 4 > mid value = 3, Step 3 will search the higher sublist with First = 2 and Last =3.

Binary Search Tracing mid -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 Search for target = 4. Step 3: Indices first= 2, last= 3, mid = (2+3)/2 = 2 -7 3 5 8 12 16 arr 1 2 4 23 33 55 6 7 mid Since target = 4 < mid value = 5, Step 4 should search the lower sublist with first = 2 and last=1. However, since first >= last , the target is not in the list and we will return index -1.

Binary Search Requires array elements to be in order Divides the array into three sections: middle element elements on one side of the middle element elements on the other side of the middle element If the middle element is the correct value, done. Otherwise, go to step 1. using only the half of the array that may contain the correct value. Continue steps 1. and 2. until either the value is found or there are no more elements to examine

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

Binary Search Algorithm int binary_search(int A[], int size, int key) { first = 0, last = size-1 // continue searching while [first, last] is not empty while (last >= first) { /* calculate the midpoint for roughly equal partition */ int mid = midpoint(first, last); // determine which subarray to search if (A[mid] < key) // change min index to search upper subarray first = mid + 1; else if (A[mid] > key ) // change max index to search lower subarray last = mid - 1; else // key found at index mid return mid; } return KEY_NOT_FOUND; // key not found }

Binary Search Program int binarySearch (int list[], int size, int key) { int first = 0, last , mid, position = -1; last = size - 1 int found = 0; while (!found && first <= last) { middle = (first + last) / 2; /* Calculate mid point */ if (list[mid] == key) { /* If value is found at mid */ found = 1; position = mid; } else if (list[mid] > key) /* If value is in lower half */ last = mid - 1; else first = mid + 1; /* If value is in upper half */ } // end while loop return position; } // end of function

Binary Search - Program

Binary Search - Program

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 8 2 1 3 4 6 5 7 index 10 9 11 12 14 13 64 25 33 51 43 53 value 84 72 93 95 97 96

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 left right 8 2 1 3 4 6 5 7 index 10 9 11 12 14 13 64 25 33 51 43 53 value 84 72 93 95 97 96 if Key v is in array, it is has index between left and right.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 left mid right 8 2 1 3 4 6 5 7 index 10 9 11 12 14 13 64 25 33 51 43 53 value 84 72 93 95 97 96 Compute midpoint and check if matching Key is in that position.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first mid last 8 2 1 3 4 6 5 7 index 10 9 11 12 14 13 64 25 33 51 43 53 value 84 72 93 95 97 96 Since 33 < 53, can reduce search interval.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first last index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Since 33 < 53, can reduce search interval.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first mid last index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Compute midpoint and check if matching Key is in that position.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first mid last index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Since 33 > 25, can reduce search interval.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first last index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 Since 33 > 25, can reduce search interval.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first last index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 mid

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 last Compute midpoint and check if matching Key is in that position.

Binary Search Demo Maintain array of Items Store in sorted order Use binary search to find Item with key = 33 first index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 value 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 last Matching Key found. Return index 4.

Binary Search Example Suppose we have the following list of length 12. We want to find the item with the value of 75. We start with the whole list and find the middle item The middle item, in list[5] is equal to 39. Since 39 is less than 75, we restrict our search to last part of the list, which is items list[6] - list[11].

Binary Search Example 2 Suppose that we are searching for item 89. Once more, given the following sorted list of length 12, Suppose that we are searching for item 89. Here is a trace of the binary search for the item 89. The item was found at location 10, and it took 3 look ups to find it.

Binary Search – Example 3 Here is a trace for finding the item 34. Here is a trace for finding the item 22. It failed. The unsuccessful search took only 4 comparisons.

Binary Search – Performance Suppose that L is a sorted list of 1000 elements, and you want to determine whether x is in L. The first iteration of the while loop searches for x in L[0] - L[999]. It compares x with L[499]. If x is less than L[499], the next iteration searches for x in L[0] - L[498].

Binary Search – Performance x is compared to L[249]. If it is greater than L[249], the new list to search is L[250] to L[498]. Each search cuts the list length in half. Because 1000 is approximately 1024, which is 2 to the 10th power, the while loop has at most 10 iterations to determine if x is in L.

Binary Search We also have looked at the binary search algorithm How much more efficient (if at all) is a binary search when compared to a sequential search? We can use “order” to help find the answer

(Time) Efficiency of an algorithm Worst case efficiency is the maximum number of steps that an algorithm can take for any input data values. Best case efficiency is the minimum number of steps that an algorithm can take for any input data values. Average case efficiency - the efficiency averaged on all possible inputs - must assume a distribution of the input - we normally assume uniform distribution (all keys are equally probable) If input has size n, efficiency will be a function of n

Binary Search Analysis Considering the worst-case for binary search: We don’t find the item until we have divided the array as far as it will divide We first look at the middle of n items, then we look at the middle of n/2 items, then n/22 items, and so on… We will divide until n/2k = 1, k is the number of times we have divided the set (when we have divided all we can, the above equation will be true) n/2k = 1 when n = 2k, so to find out how many times we divided the set, we solve for k k = log2 n Thus, the algorithm takes O(log n), the worst-case For Average case is log n – 1 i.e. one less

How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items ? 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

How Fast is a Binary Search? (con’t) List has 512 items 1st try - 256 items 2nd try - 128 items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item List has 250 items 1st try - 125 items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item

What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries 32 = 25 and 512 = 29 8 < 11 < 16 23 < 11 < 24 128 < 250 < 256 27 < 250 < 28

A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items long? 210 = 1024 213 = 8192 211 = 2048 214 = 16384 212 = 4096 215 = 32768 So, it will take only 15 tries!

Log2 n Efficiency We say that the binary search algorithm runs in log2n time. (Also written as lg n) Log2n means the log to the base 2 of some value of n. 8 = 23 log28 = 3 16 = 24 log216 = 4 There are no algorithms that run faster than log2 n time

Binary Search

Linear (Sequential) Search

Worst Case Efficiency for Linear Search Get the value of target, n, and the list of n values 1 Set index to 1 1 Set found to false 1 Repeat steps 5-8 until found = true or index > n n 5 if the value of listindex = target then n Output the index 0 Set found to true 0 8 else Increment the index by 1 n 9 if not found then 1 10 Print a message that target was not found 0 Stop 1 Total 3n+5

Analysis of Sequential Search Time efficiency Best-case : 1 comparison target is found immediately Worst-case: 3n + 5 comparisons Target is not found Average-case: 3n/2+4 comparisons Target is found in the middle Space efficiency How much space is used in addition to the input?

Order of Magnitude Worst-case of Linear search: Simplification: 3n+5 comparisons Are these constants accurate? Can we ignore them? Simplification: ignore the constants, look only at the order of magnitude n, 0.5n, 2n, 4n, 3n+5, 2n+100, 0.1n+3 are all linear we say that their order of magnitude is n 3n+5 is order of magnitude n: 3n+5 = (n) 2n +100 is order of magnitude n: 2n+100=(n) 0.1n+3 is order of magnitude n: 0.1n+3=(n) ….

Comparing Search Algorithms We know sequential search is O(n) worst-case binary search is O(log2 n) worst-case Which is better? Given n = 1,000,000 items O(n) = O(1,000,000) /* sequential */ O(log2 n) = O(19) /* binary */ Clearly binary search is better in worst-case for large values of n, but there is always trade-offs that must be considered Binary search requires the array to be sorted If the item to be found is near the extremes of the array, sequential may be faster

Binary Search Tradeoffs

Comparison Sequential and Binary The sequential search starts at the first element in the list and continues down the list until either the item is found or the entire list has been searched. If the wanted item is found, its index is returned. So it is slow. Sequential search is not efficient because on the average it needs to search half a list to find an item. A Binary search is much faster than a sequential search. Binary search works only on an ordered list. Binary search is efficient as it disregards lower half after a comparison.

Searching Unordered Link List Let LIST be a linked list in memory pointed to by a pointer HEAD Suppose a specific ITEM of information is given. ITEM is the key value to be searched Two cases LIST is Unordered i.e. not sorted LIST is Ordered i.e. sorted We will search ITEM in LIST by traversing through the list using a pointer variable PTR Comparing ITEM with the contents of each node of the LIST one by one until the ITEM is found or the LIST is completely traversed

Searching Unordered Link List ListNode* Search_List (int item) { // This algorithm finds the location Loc of the node in an Unordered linked // list where It first appears in the list or sets loc = NULL ListNode *ptr, *loc; int found = 0; ptr = head; while (ptr != NULL) && (found == 0) { if (ptr->value == item) { loc = ptr; found = 1; } // end if else ptr = ptr->next; } // end of while if (found == 0); loc = NULL return loc; } // end of function Search_List

Complexity Analysis Complexity of this algorithm is same as that of linear (sequential) algorithm Worst-case running time is approximately proportional to the number n of elements in LIST i.e. O(n) Average-case running time is approximately proportional to n/2 (with the condition that Item appears once in LIST but with equal probability in any node of LIST i.e. O(n)

Searching Ordered Link List ListNode* Search_List (int item) { // This algorithm finds the location Loc of the node in an Ordered linked list where It first appears in the list or sets loc = NULL ListNode *ptr, *loc; ptr = head; loc = NULL; while (ptr != NULL) { if (ptr->value < item) { ptr = ptr -> next; else if (ptr->value == item) loc = ptr; } // end while return loc; } // end of function Search_List

Complexity Analysis Complexity of this algorithm is same as that of linear (sequential) algorithm Worst-case running time is approximately proportional to the number n of elements in LIST i.e. O(n) Average-case running time is approximately proportional to n/2 (with the condition that Item appears once in LIST but with equal probability in any node of LIST i.e. O(n)

Ordered Linked List and Binary Search With a sorted linear array, we can apply a binary search whose running time is proportional to log2 n A binary search algorithm cannot be applied to a Ordered (Sorted) Linked List Since there is no way of indexing the middle element in the list This property is one of the main drawback in using a linked list as a data structure 56

Summary Binary Search Implementation of Binary search Concept Algorithm Implementation of Binary search Complexity of Binary Search Comparison of Linear and Binary Search Searching Unordered Linked List Searching Ordered Linked List