Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
CSE Lecture 3 – Algorithms I
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
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 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Analysis of Algorithm.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
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.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Data Structures Using C++
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
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
CSE 1320 Search, Sort and Strings
Search Algorithms Sequential Search (Linear Search) Binary Search
Linear and Binary Search
Adapted from Pearson Education, Inc.
Lecture 14: binary search and complexity reading:
Chapter 8 Search and Sort
Lecture 15: binary search reading:
Searching and Sorting Arrays
Ken Lambert & Doug Nance
Searching CLRS, Sections 9.1 – 9.3.
Data Structures: Searching
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
The Binary Search by Mr. Dave Clausen
Presentation transcript:

Searching Sequential Search Binary Search

Sequential Search Sequential search is to look into the key one after another until the target is found If the list of data does not contain the target, the search performs until the last item has been reached. Although, sequential search, by its nature, is easy to program, it is the slowest technique Sequential search takes at most n comparisons and n/2 comparisons on average.

Search By Guessing: Binary Search The key idea for Binary Search is that elements in the list need to be stored before the search begins The target is compared with the middle element If lucky, the target is located in the middle –Therefore, the search finished with successful result If not, result indicates which half of the list contains target (The other half will be discarded.) Process goes until the target is found or the number of potential elements becomes zero.

Binary Search vs Sequential Search A binary search of a file with n records takes at most: And on average approximately: Sequential search takes at most n comparisons and n/2 comparisons on average. With 1,000 records, it takes at most: –10 comparisons for Binary Search –1,000 comparisons for Sequential Search. Note: lg is logarithm function to the base 2.

Binary Search vs Sequential Search Doubling number of records will cost: –One more comparison for Binary Search –But double number of comparisons for Sequential search Binary search is said to be O(lg n) While sequential search is O(n). on average and could be 2,000 comparisons.

Finding Things Sequential search implies looking at every record Binary search approximately takes What if there is no record containing the requested file? What if we think there might be more than one record containing the key? However: This lecture assume searching for primary. (no duplication involved)

Finding a Record by Its Key List sorted by its key (ascending) Unsorted List Sequential Search Binary Search

Psuedo: Sequential Search int sequential_search(Data[], n, Target) { For i = 0 to n-1 if(Target == Data[i]) return i //Found Target Next i return -1; //Target is not found }

Psuedo: Binary Search int binary_search(Data[], n, Target) { start=0; end=n-1; while(TRUE){ mid = start + ((end-start)/2); if(mid==start) { if (Data[mid]==Target) return mid; //FOUND if (Data[end]==Target) return end; //FOUND return -1; //Target Not Found } if (Data[mid] == Target) return mid; //FOUND else if(Data[mid] < Target) start = mid+1; //Discard Left else end = mid - 1; //Discard Right }

Programming Time Given a record of Suppose, we have 20 students, given Can you perform both searches by student’s name? typedef struct { char name[50]; int score; } STD_SCORE; STD_SCORE std_score[20];

String Compare Functions Try these two functions for string comparision The upper compares two strings, ignoring case The lower compare first n characters, ignoring case int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n);