Review Arrays Strings Array Elements Accessing array elements

  • Slides: 33
Download presentation
Review � Arrays & Strings � Array Elements � Accessing array elements � Declaring

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

Searching �Introduction to Searching �External and Internal Searching �Types of Searching �Linear or sequential

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

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

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

�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

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

Records and their keys 7

Analysis �Searching for the keys that locate records is often 8 the most time-consuming

Analysis �Searching for the keys that locate records is often 8 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

External and Internal Searching �The searching problem falls naturally into two cases External Searching

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

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

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 11 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

Sequential Search on an Ordered List �In this case, we have an ordered list

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 13 Let A be an array of n elements, A[1],

Algorithm for Linear Search 13 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 = – 1. 2. 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

Sequential Search of Ordered vs. Unordered List �Let’s do a comparison. �If the order

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

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,

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

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 18 when it is compared

Binary Search �Binary search is an extremely efficient algorithm 18 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

�In only twenty steps, this method will locate any requested key in a list

�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

20 1. Find the middle element of the array (i. e. , n/2 is

20 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

Algorithm for Binary Search �Let A be an array of n elements A[1], A[2],

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

22 1. Input an array A of n elements and “data” to be searched

22 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 + 1 6. 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

How a Binary Search Works Always look at the center value. Each time you

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

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 ? � 1 st try - list has 16 items � 2 nd try - list has 8 items � 3 rd try - list has 4 items � 4 th try - list has 2 items � 5 th try - list has 1 item 24

How Fast is a Binary Search? (con’t) 25 List has 250 items List has

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

What’s the Pattern? �List of 11 took 4 tries �List of 32 took 5

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 = 25 and 512 = 29 26

A Very Fast Algorithm! �How long (worst case) will it take to find an

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

log 2 n Efficiency �After 1 bisection N/2 �After 2 bisections N/4 = N/22

log 2 n Efficiency �After 1 bisection N/2 �After 2 bisections N/4 = N/22 . . . �After i bisections N/2 i =1 items � i = log 2 N 28 item

Lg n Efficiency �We say that the binary search algorithm runs in log 2

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 = 23 lg 8 = 3 16 = 24 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

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 -

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 -

�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

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