Programming and Data Structures Debasis Samanta Computer Science
Programming and Data Structures Debasis Samanta Computer Science & Engineering Indian Institute of Technology Kharagpur Spring-2017
Lecture #11 Searching Techniques CS 11001 : Programming and Data Structures 2 Lecture #11: © DSamanta
Today’s discussion… * Searching Techniques * Sequential search with arrays • • * Binary search Interpolation search Sequential search with linked lists CS 11001 : Programming and Data Structures 3 Lecture #11: © DSamanta
Searching Techniques CS 11001 : Programming and Data Structures 4 Lecture #11: © DSamanta
CS 11001 : Programming and Data Structures 5 Lecture #11: © DSamanta
Linear Search CS 11001 : Programming and Data Structures 6 Lecture #11: © DSamanta
Sequential Search with Arrays CS 11001 : Programming and Data Structures 7 Lecture #11: © DSamanta
Flowchart: Sequential Search with Array CS 11001 : Programming and Data Structures 8 Lecture #11: © DSamanta
Example: Sequential Search with Array int main() { int A[10], i, n, K, flag = 0; printf("Enter the size of an array: "); scanf("%d", &n); printf("Enter the elements of the array: "); for(i=0; i < n; i++) scanf("%d", &A[i]); printf("Enter the number to be searched: "); scanf("%d", &K); for(i=0; i<n; i++){ if(a[i] == K){ flag = 1; break; } } if(flag == 0) printf("The number is not in the list"); else printf("The number is found at index %d", i); return 0; } CS 11001 : Programming and Data Structures 9 Lecture #11: © DSamanta
Complexity Analysis • Case 1: The key matches with the first element • T(n) = 1 • Case 2: Key does not exist • T(n) = n • Case 3: The key is present at any location in the array CS 11001 : Programming and Data Structures 10 Lecture #11: © DSamanta
Complexity Analysis : Summary Case Number of key comparisons Asymptotic complexity Remark Case 1 T(n) = O(1) Best case Case 2 T(n) = n T(n) = O(n) Worst case T(n) = O(n) Average case Case 3 CS 11001 : Programming and Data Structures 11 Lecture #11: © DSamanta
Binary Search CS 11001 : Programming and Data Structures 12 Lecture #11: © DSamanta
The Technique CS 11001 : Programming and Data Structures 13 Lecture #11: © DSamanta
Flowchart: Binary Search with Array CS 11001 : Programming and Data Structures 14 Lecture #11: © DSamanta
Binary Search (with Iteration) #include <stdio. h> int main() { int i, l, u, mid, n, K, data[100]; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integers in sorted ordern", n); for (i = 0; i < n; i++) scanf("%d", &array[i]); printf("Enter value to findn"); scanf("%d", &K); l = 0; u = n - 1; mid = (l+u)/2; CS 11001 : Programming and Data Structures Contd… 15 Lecture #11: © DSamanta
Binary Search (with Iteration) while (l <= u) { if (data[mid] < K) l = mid + 1; else if (data[mid] == K) { printf("%d found at location %d. n", search, mid+1); break; } else u = mid - 1; mid = (l + u)/2; } if (l > u) printf("Not found! %d is not present in the list. n", K); return 0; } CS 11001 : Programming and Data Structures 16 Lecture #11: © DSamanta
Binary Search (with Recursion) #include<stdio. h> int main(){ int data[100], i, n, K, flag, l, u; printf("Enter the size of an array: "); scanf("%d", &n); printf("Enter the elements of the array in sorted order: " ); for(i=0; i<n; i++) scanf("%d", &a[i]); printf("Enter the number to be search: "); scanf("%d", &K); l=0, u=n-1; flag = binary. Search(data, n, K, l, u); if(flag==0) printf("Number is not found. "); else printf("Number is found. "); Contd… return 0; } CS 11001 : Programming and Data Structures 17 Lecture #11: © DSamanta
Binary Search (with Recursion) int binary(int a[], int n, int K, int l, int u){ int mid; if(l<=u){ mid=(l+u)/2; if(K==a[mid]){ return(1); } else if(m<a[mid]){ return binary. Search(a, n, K, l, mid-1); } else return binary. Search(a, n, m, mid+1, u); } else return(0); } CS 11001 : Programming and Data Structures 18 Lecture #11: © DSamanta
Complexity Analysis CS 11001 : Programming and Data Structures 19 Lecture #11: © DSamanta
Complexity Analysis: Binary Search Let n be the total number of elements in the list under search and there exist an integer k such that: - • For successful search: - • • If , then the binary search algorithm requires at least one comparison and at most k comparisons. For unsuccessful search: - • • If If , then the binary search algorithm requires k comparisons. , then the binary search algorithm requires either k-1 or k number of comparisons. CS 11001 : Programming and Data Structures 20 Lecture #11: © DSamanta
Complexity Analysis: Binary Search • Best case T(n) = 1 • Worst case T(n) = CS 11001 : Programming and Data Structures 21 Lecture #11: © DSamanta
Complexity Analysis: Binary Search • Average Case • Successful search: - • Unsuccessful search: - CS 11001 : Programming and Data Structures 22 Lecture #11: © DSamanta
Interpolation Search CS 11001 : Programming and Data Structures 23 Lecture #11: © DSamanta
Interpolation Search CS 11001 : Programming and Data Structures 24 Lecture #11: © DSamanta
Complexity Analysis: Interpolation Search Best case Successful Worst case Average case 1 Interpolation search Unsuccessful CS 11001 : Programming and Data Structures 25 Lecture #11: © DSamanta
Comparision: Successful Search CS 11001 : Programming and Data Structures 26 Lecture #11: © DSamanta
Comparision: Unsuccessful Search CS 11001 : Programming and Data Structures 27 Lecture #11: © DSamanta
Sequential Search with Linked List CS 11001 : Programming and Data Structures 28 Lecture #11: © DSamanta
Sequential Search with Linked List CS 11001 : Programming and Data Structures 29 Lecture #11: © DSamanta
Flow Chart: Sequential Search with LL Start temp = header temp = temp->next while temp != NULL Print Unsuccessful T N temp->data == key Y Print Success Return temp CS 11001 : Programming and Data Structures 30 Lecture #11: © DSamanta
Example: Sequential Search with LL #include <stdio. h> #include <stdlib. h> struct node { int data; struct node *next; }; int main() { struct node *header = NULL; int K, n; printf("Enter the number of nodes: "); scanf("%d", &n); printf("n. Displaying the listn"); generate(header, num); printf("n. Enter key to search: "); scanf("%d", &key); search. Binary(header, K); delete(header); return 0; } CS 11001 : Programming and Data Structures 31 Lecture #11: © DSamanta
Example: Linear Search with LL void generate(struct node *head, int n) { int i; struct node *temp; for (i = 0; i < num; i++) { temp = (struct node *)malloc(sizeof(struct node)); temp->data = rand() % n; if (*header == NULL) { *header = temp; temp->next = NULL; } else { temp->next = header; header = temp; } printf("%d ", temp- >data); } } CS 11001 : Programming and Data Structures 32 Lecture #11: © DSamanta
Example: Linear Search with LL void search. Binary(struct node *temp, int K) { while (temp != NULL) { if (temp->data == K) { printf("key foundn"); return; } else temp = temp->next; } printf("Key not foundn"); } void delete(struct node *header) { struct node *temp; temp = header; while (temp != NULL) { temp = temp->next; free(header); header = temp; } } CS 11001 : Programming and Data Structures 33 Lecture #11: © DSamanta
Complexity Analysis Case Number of key comparisons Asymptotic complexity Remark Case 1 T(n) = O(1) Best case T(n) = O(n) Average case T(n) = O(n) Worst case 34 Lecture #11: © DSamanta Case 2 Case 3 T(n) = n CS 11001 : Programming and Data Structures
Any question? You may post your question(s) at the “Discussion Forum” maintained in the course Web page. CS 11001 : Programming and Data Structures 35 Lecture #11: © DSamanta
Problems to ponder… 1. What will be the time complexity of linear search with array if the array is already in sorted order? 2. What will be the outcome, if Binary search technique is applied to an array where the data are not necessarily in sorted order? 3. Whether the Binary search technique is applicable to a linked list? If so, how? 4. In Binary Search, the mid location is calculated and then either left or right part of the mid location is searched further, if there is no match at the middle is found. As an alternative to check at middle, the location at one-third (or two-third) position of the array is chosen. Such a searching can be termed as Ternary Search. Modify the Binary Search algorithm to Ternary Search algorithm. Which algorithm gives result faster? 5. If T(n) denotes the number of comparisons required to search a key element in a list of n elements, then a) express T(n) as recursion formula and b) solve T(n). CS 11001 : Programming and Data Structures 36 Lecture #11: © DSamanta
Problems to ponder… 6. Out of sequential search and binary search, which is faster? Why? 7. Whether binary search technique can be applied to search a string in a list od strings stored in an array? If so, revise the Binary search algorithm accordingly. 8. A structure is defined as follows. struct Record { int Roll. No; char name[20]; struct Date { int dd; int mm; int yy; } dob; } suppose, 100 records of type Record are stored in an array say, struct Record students[100]; We have to find the student(s), whose date of birth is given in the form dd/mm/yy. How you can search the array students? CS 11001 : Programming and Data Structures 37 Lecture #11: © DSamanta
Problems for Practice… *You can check the Moodle course management system for a set of problems for your own practice. • Login to the Moodle system at http: //cse. iitkgp. ac. in/ • Select “PDS Spring-2017 (Theory) in the link “My Courses” • Go to Topic 11: Practice Sheet #11 : Searching Technique *Solutions to the problems in Practice Sheet #11 will be uploaded in due time. CS 10001 : Programming and Data Structures 38 Lecture #07: © DSamanta
If you try to solve problems yourself, then you will learn many things automatically. Spend few minutes and then enjoy the study. CS 10001 : Programming and Data Structures 39 Lecture #07: © DSamanta
- Slides: 39