240-222 CPT: Search/171 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Introduction to Algorithms Quicksort
College of Information Technology & Design
MATH 224 – Discrete Mathematics
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
HST 952 Computing for Biomedical Scientists Lecture 9.
Chapter 1 – Basic Concepts
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
 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
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
EENG212 Algorithms and Data Structures
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (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.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
1 Searching and Sorting Linear Search Binary Search.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
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.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Searching CSCE 121 J. Michael Moore.
Searching.
Searching CLRS, Sections 9.1 – 9.3.
Searching.
Presentation transcript:

CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation, and some complexity analyses 17. Searching

CPT: Search/172 Overview: 1. Searching Definition 2. External/Internal Searching 3. Simplifying the Search 4. Analysing Searching 5. Linear Search 6. Binary Search 7. Comparison of Searching Algorithms

CPT: Search/ Searching Definition l We are given a collection of n elements: a 0, a 1,... a n-1 l Each a i consists of two parts – a unique ID, or key, and some data. l Searching is the process of finding an a i such that its key equals some specified key value, k.

CPT: Search/ External/Internal Searching l Searching algorithms fall into two broad categories: –external searching (large amount of data on disk) –internal searching (small amount of data in memory)

CPT: Search/ External Searching l The data is too large to be all loaded into memory at once. l Scan all the data files to find the requested item. l Try to minimise the number of blocks read.

CPT: Search/ Internal Searching l Traverse the data structure holding the items. l Arrays, lists and trees can be used as data structures. l Try to minimise the number of key comparisons.

CPT: Search/ Simplifying the Search 3.1. Internal Search (arrays) 3.2. Simplified Search Data Structure 3.3. Search Function Interface 3.4. Search Driver

CPT: Search/ Internal Search (arrays) l Array-based search data structure: #define SIZE 100 typedef data_item ??/* e.g. string */ struct elem { int key; data_item s; } struct elem a[SIZE];

CPT: Search/179 Pictorially: jimjanebobannjanejim key data item a[0]a[1]a[2]a[3]a[4]

CPT: Search/ Simplified Search Data Structure l Remove keys l Represent the data items as integers l Each integer (data item) is unique: no duplicates –can use data items as keys l e.g a[0]a[1]a[2]a[3]...

CPT: Search/ Search Function Interface int search(int array[], int key, int size) { /*... */ } l The function examines the array looking for an item containing key. –Success: return array index –Failure: return -1

CPT: Search/ Search Driver /* Search an integer array */ #include #define SIZE 100 int search(int [], int, int); void main() { int a[SIZE], x, searchkey, index; for (x = 0; x < SIZE; x++) a[x] = 2 * x; printf("Enter integer search key:\n"); scanf("%d", &searchkey); : continued

CPT: Search/1713 index = search(a, searchkey, SIZE); if (index != -1) printf("Array index %d\n", index); else printf("Value not found\n"); } int search(int array[], int key, int size) { /* search implementation */ }

CPT: Search/ Analysing Search l Searching algorithms should be both space and time efficient. l With internal searching, the critical factor is the number of key comparisons, C. l For each searching algorithm, consider its best, worst and average case performance in terms of C.

CPT: Search/ Linear Search 5.1. Linear Search Algorithm 5.2. Linear Search Function 5.3. Linear Search Program 5.4. Analysis of Linear Search 5.5. Sorted Linear Search 5.6. Recursive Linear Search

CPT: Search/ Linear Search Algorithm l Move along the data structure, comparing the search key, k, to the key value of the current item until: –a match is found or –all the data structure has been considered

CPT: Search/ Linear Search Function int linear_search(int array[], int key, int size) { int n; for (n = 0; n < size; n++) if (array[n] == key) return n; return -1; }

CPT: Search/ Linear Search Program Fig 6.18 /* Linear search of an array */ #include #define SIZE 100 int linear_search(int [], int, int); void main() { int a[SIZE], x, searchkey, index; for (x = 0; x < SIZE; x++) a[x] = 2 * x; printf("Enter integer search key:\n"); scanf("%d", &searchkey); : continued

CPT: Search/1719 index = linear_search(a, searchkey, SIZE); if (index != -1) printf("Array index %d\n", index); else printf("Value not found\n"); }

CPT: Search/1720 Execution Enter Integer Search Key: 36 Array index 18 Enter integer search key: 37 Value not found

CPT: Search/ Analysis of Linear Search l Consider an array with n items. l In the best case, find a match at the start of the array: C min = 1 l In the worst case, all items are examined: C max = n

CPT: Search/1722 l In the average case, about half of the items are considered: C average = n/2 = O(n)

CPT: Search/1723 Meaning of O() l Read O() as “about”, where constants and small values are ignored. Concentrate on large changes. l For example: –5n + 2= O(n) –5n 2 + n= O(n 2 ) –6= O(1)/* constant */ l O() is useful for giving rough estimates.

CPT: Search/ Sorted Linear Search int sl_search(int array[], int key, int size) { int n; for (n = 0; n key) return -1;/* larger key found */ return -1; }

CPT: Search/1725 l The actual speed of the function will increase (for some arrays) but the average complexity remains at O(n)

CPT: Search/ Recursive Linear Search int rl_search(int array[], int key, int index, int size) { if (index >= size) return -1; if (array[index] == key) return index; else return( rl_search(array, key, index+1, size) ); }

CPT: Search/1727 Call from main() : element = rl_search(a, searchkey, 0, SIZE) /* 0 is initial index */

CPT: Search/ Binary Search 6.1. Binary Search Algorithm 6.2. Execution of Binary Search 6.3. Binary Search Function 6.4. Analysis of Binary Search 6.5. Recursive Binary Search Function

CPT: Search/ Binary Search Algorithm l A divide-and-conquer algorithm l Search for key value k: 1. Take middle item of array segment: a mid 2. If k == a mid 's key, then the search is successful :

CPT: Search/ Otherwise, if the range of array items under consideration is empty then the search has failed 4. If k < a mid 's key then restrict search to lower half of array and go to step 1 5. If k > a mid 's key then restrict search to upper half of array and go to step 1

CPT: Search/ Execution of Binary Search l Searching for 15 in 2, 4, 6, 7, 10, 11, 15, 17, 20, 29, 30 l Since 15 > the middle key (11), consider its upper half: 15, 17, 20, 29, 30 l Since 15 < the middle key (20), consider its lower half: 15, 17 l 15 == middle item, success.

CPT: Search/ Binary Search Function int binary_search(int array[], int key, int size) { int low = 0, high = size - 1, mid; while ( low array[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ }

CPT: Search/ Analysis of Binary Search l Consider an array with 2 k items. l At each iteration: –either one or two key comparisons are made –the number of items under consideration is halved l At an array range of size 1, either: –found the item, or –item not in array.

CPT: Search/1734 l It takes k iterations to reach array range of size 1: 2 k items ฎ 1 itemin k steps so k items ฎ 1 itemin log 2 k steps l Thus, for an array of size n, it takes (about) log 2 n steps/iterations.

CPT: Search/1735 Best Case l Find a match in the first iteration: C min = 2

CPT: Search/1736 Worst Case l Key is not in the array l Array is divided in half log 2 n times l Each iteration requires roughly 2 key comparisons l C max ญ 2 * no. of iterations 2 * log 2 n =O(log 2 n)

CPT: Search/1737 Average Case l Need to consider all possible cases: –matches at all positions –misses at all positions l The result: C average ญ 1.8 * logn = O(log 2 n) l Close to worst case performance

CPT: Search/ Recursive Binary Search Function int rb_search(int array[], int left, int right, int key) { int mid; if (left > right) /* nowhere to look */ return -1; mid = (left+right)/2; if (array[mid] == key) return mid; if (array[mid) < key) return( rb_search(array, mid+1, right, key) ); else return( rb_search(array, left, mid-1, key) ); }

CPT: Search/1739 The initial call from main(): element = rb_search(a, 0, SIZE-1, searchkey); l Average complexity remains at O(log 2 n)

CPT: Search/ Comparison of Searching Algorithms l Linear search is O(n) l Binary search is O(log 2 n) l Plug in values of n, to see that binary search is better (faster).

CPT: Search/1741 l The actual speed of linear search can be improved by ordering elements. However it is still O(n). l For an array of sorted elements, binary search is preferable l If the array is unsorted, binary search cannot be used.