Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

CSE Lecture 3 – Algorithms I
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.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Sequential Search slides. Searching Searching : –Information retrieval is one of the most important application of computers. –EG: Looking for a Name.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Elementary Data Structures and Algorithms
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.
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.
Searching and Sorting Topics Sequential Search on an Unordered File
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
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.
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.
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 م. م علي عبد الكريم حبيب.
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.
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 Structure Introduction.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
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.
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.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Sorting and Searching Bubble Sort Linear Search Binary Search.
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.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
CMPT 120 Topic: Searching – Part 2
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Arrays
Manipulating lists of data
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
Sequential Search slides
Data Structures: Searching
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Principles of Computing – UFCFA3-30-1
Presentation transcript:

Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure String Array of Strings Examples

Searching Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search Algorithms for Binary Search 2

Introduction Information retrieval is one of the most important applications of computers We are given a name and are asked for an associated telephone listing We are given an account number and are asked for the transactions occurring in that account We are given an employee name or number and are asked for the employee records 3

Introduction Information retrieval is one of the most important applications of computers We are given a name and are asked for an associated telephone listing We are given an account number and are asked for the transactions occurring in that account We are given an employee name or number and are asked for the employee records 4

Searching is a process of checking and finding an element from a list of elements If we want to find the presence of an element “data” in A, then we have to search for it The search is successful if data does appear in A and unsuccessful otherwise A question you should always ask when selecting a search algorithm is “How fast does the search have to be?” The reason is that, in general, the faster the algorithm is, the more complex it is. Bottom line: you don’t always need to use or should use the fastest algorithm 5

Key In these examples, we are given one piece of information, which we shall call a key We are asked to find a record that contains other information associated with the key We shall allow both the possibility that there is more than one record with the same key and that there is no record with the given key 6

Records and their keys 7

Analysis Searching for the keys that locate records is often the most time-consuming action in a program therefore, the way records are arranged and the choice of method used for searching can make a substantial difference in the program’s performance For this reason, we should check that how much work is done by each of the algorithms we develop We shall find that counting the number of times that one key is compared with another gives us an excellent measure of the total amount of work that the algorithm will do and of the total amount of computer time it will require when it is run 8

External and Internal Searching The searching problem falls naturally into two cases External Searching If there are many records, perhaps each one quite large, then it will be necessary to store the records in files on disk or tape, external to the computer memory Internal Searching In this case, the records to be searched are stored entirely within the computer memory 9

Types of Searching There are two types of searching techniques: Linear or Sequential Searching Binary Searching Linear or Sequential Searching In linear search, each element of an array is read one by one sequentially It is compared with the desired element A search will be unsuccessful if all the elements are read and the desired element is not found 10

Sequential Search on an Unordered List In this case, we have an unordered list of items/elements We check each and every element in the list sequentially and it is compared with the desired element Algorithm Get the search criterion (key) Get the first record from the file While ( (record != key) and (still more records) ) Get the next record End_while If ( record = key ) Then success Else there is no match in the file End_else 11

Sequential Search on an Ordered List In this case, we have an ordered list of elements We check each and every element in the list sequentially and it is compared with the desired element Algorithm: Get the search criterion (key) Get the first record from the file While ( (record < key) and (still more records) ) Get the next record End_while If ( record = key ) Then success Else there is no match in the file End_else 12

Algorithm for Linear Search Let A be an array of n elements, A[1],A[2],A[3], A[n]. “data” is the element to be searched. Then this algorithm will find the location “loc” of data in A. Set loc = – 1,if the search is unsuccessful. 1. Input an array A of n elements and “data” to be searched and initialize loc = – Initialize i = 0; and repeat through step 3 if (i < n) by incrementing i by one. 3. if (data = A[i]) (a) loc = i (b) GOTO step 4 4. if (loc > 0) (a) Display “data is found and searching is successful” 5. else (a) Display “data is not found and searching is unsuccessful” 6. exit 13

Sequential Search of Ordered vs. Unordered List Let’s do a comparison. If the order was ascending alphabetical on customer’s last names, how would the search for Shahab Ali on the ordered list compare with the search on the unordered list? Unordered list if Shahab Ali was in the list? if Shahab Ali was not in the list? Ordered list if Shahab Ali was in the list? if Shahab Ali was not in the list? 14

Ordered vs. Unordered (cont) How about Mohammad Waqas? Unordered if Mohammad Waqas was in the list? if Mohammad Waqas was not in the list? Ordered if Mohammad Waqas was in the list? if Mohammad Waqas was not in the list? 15

Ordered vs. Unordered (cont) Observation: the search is faster on an ordered list Also, keep in mind that the list has to first be placed in order for the ordered search. Conclusion: the efficiency of these algorithms is roughly the same. So, if we need a faster search, we need a completely different algorithm. How else could we search an ordered file? 16

Binary Search Sequential search is easy to write and efficient for short lists, but a disaster for long ones Imagine trying to find the name “Ahmad Tahir” in a large telephone book by reading one name at a time starting at the front of the book To find any entry in a long list, there are far more efficient methods, provided that the keys in the list are already sorted into order If we have an ordered list, we can use a different strategy The binary search gets its name because the algorithm continually divides the list into two parts 17

Binary Search Binary search is an extremely efficient algorithm when it is compared to linear search Binary search technique searches “data” in minimum possible comparisons First compare the target key with one in the center of the list and then restrict our attention to only the first or second half of the list, depending on whether the target key comes before or after the central one With one comparison of keys we thus reduce the list to half its original size Continuing in this way, at each step, we reduce the length of the list to be searched by half 18

In only twenty steps, this method will locate any requested key in a list containing more than a million elements This approach of course requires that the elements in the list already be in completely order Suppose we have a sorted array of n elements, then apply the following steps to search an element 19

1. Find the middle element of the array (i.e., n/2 is the middle element if the array or the sub-array contains n elements) 2. Compare the middle element with the data to be searched, then there are following three cases (a) If it is a desired element, then search is successful (b) If it is less than desired data, then search only the first half of the array, i.e., the elements which come to the left side of the middle element (c) If it is greater than the desired data, then search only the second half of the array, i.e., the elements which come to the right side of the middle element Repeat the same steps until an element is found or exhaust the search area 20

Algorithm for Binary Search Let A be an array of n elements A[1],A[2],A[3], A[n] “Data” is an element to be searched “mid” denotes the middle location of a segment (or array or sub-array) of the element of A LB and UB is the lower and upper bound of the array which is under consideration 21

1. Input an array A of n elements and “data” to be searched 2. LB = 0, UB = n; mid = int ((LB+UB)/2) 3. Repeat step 4,5 and 6 while (LB <= UB) and (A[mid] ! = data) 4. If (data < A[mid]) (a) UB = mid–1 5. Else (a) LB = mid Mid = int ((LB + UB)/2) 7. If (A[mid]== data) (a) Display “the data found” 8. Else (a) Display “the data is not found” 9. Exit 22

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 ? 23

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 24

How Fast is a Binary Search? (con’t) List has 250 items 1st try 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 List has 512 items 1st try items 2nd try 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 25

What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries List of 250 took 8 tries List of 512 took 9 tries 32 = 2 5 and 512 =

A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items? 2 10 = = = = = = So, it will take only 15 tries! 27

log 2 n Efficiency After 1 bisectionN/2 items After 2 bisectionsN/4 = N/2 2 items... After i bisectionsN/2 i =1 item i = log 2 N 28

Lg n Efficiency We say that the binary search algorithm runs in log 2 n time. (Also written as lg n) Lg n means the log to the base 2 of some value of n. 8 = 2 3 lg 8 = 316 = 2 4 lg 16 = 4 There are no algorithms that run faster than lg n time. 29

Example Suppose we have an array of 7 elements Following steps are generated if we binary search a data = 45 from the above array Step 1: LB = 0; UB = 6 mid = (0 + 6)/2 = 3 A[mid] = A[3] = 30 30

Step 2: Since (A[3] < data) - i.e., 30 < 45 - reinitialise the variable LB, UB and mid LB = mid+1 LB = 4 UB = 6 mid = (4 + 6)/2 = 5 A[mid] = A[5] = 45 31

Step 3: Since (A[5] == data) - i.e., 45 == 45 - searching is successful 32

Summary Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search Algorithms for Binary Search 33