Introduction to Search Algorithms Introduction to Search Algorithms

  • Slides: 25
Download presentation
Introduction to Search Algorithms

Introduction to Search Algorithms

Introduction to Search Algorithms � Search: � Two locate an item in a list

Introduction to Search Algorithms � Search: � Two locate an item in a list of information algorithms we will examine: ◦ Linear search ◦ Binary search

Linear Search � Also called the sequential search � Starting at the first element,

Linear Search � Also called the sequential search � Starting at the first element, this algorithm sequentially steps through an array examining each element until it locates the value it is searching for.

Linear Search - Example � Array numlist contains: 17 � Searching 23 5 11

Linear Search - Example � Array numlist contains: 17 � Searching 23 5 11 2 29 3 for the value 11, linear search examines 17, 23, 5, and 11 � Searching for the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3

Linear Search � Algorithm: set found to false; set position to – 1; set

Linear Search � Algorithm: set found to false; set position to – 1; set index to 0 while index < number of elts. and found is false if list[index] is equal to search value found = true position = index end if add 1 to index end while return position

#include<stdio. h> int main() { int N, key; int A[N]; int i, indx=-1; printf("enter

#include<stdio. h> int main() { int N, key; int A[N]; int i, indx=-1; printf("enter the number of elements: "); scanf("%d", &N); printf("enter the array's elements: "); for(i=0; i<N; i++) { scanf("%d", &A[i]); } printf("enter the key: "); scanf("%d", &key); for(i=0; i<N; i++) { if(A[i]==key) { indx=i; break; } } if(indx==-1) printf("the key does not exist"); } A Linear Search Function else printf("the key does exist at index: %d ", indx); return 0;

Linear Search - Tradeoffs � Benefits: ◦ Easy algorithm to understand ◦ Array can

Linear Search - Tradeoffs � Benefits: ◦ Easy algorithm to understand ◦ Array can be in any order � Disadvantages: ◦ Inefficient (slow): for array of N elements, examines N/2 elements on average for value in array, N elements for value not in array

Binary Search 1. 2. 3. Requires array elements to be in order Divides the

Binary Search 1. 2. 3. Requires array elements to be in order Divides the array into three sections: ◦ middle element ◦ elements on one side of the middle element ◦ elements on the other side of the middle element If the middle element is the correct value, done. Otherwise, go to step 1. using only the half of the array that may contain the correct value. Continue steps 1. and 2. until either the value is found or there are no more elements to examine

Binary Search - Example � Array numlist 2 contains: 2 � Searching 3 5

Binary Search - Example � Array numlist 2 contains: 2 � Searching 3 5 11 17 23 29 for the value 11, binary search examines 11 and stops � Searching for the value 7, linear search examines 11, 3, 5, and stops

Pseudocode of Binary Search Set first index to 0. Set last index to the

Pseudocode of Binary Search Set first index to 0. Set last index to the last subscript in the array. Set found to false. Set position to -1. While found is not true and first is less than or equal to last Set middle to the subscript half-way between array[first] and array[last]. If array[middle] equals the desired value Set found to true. Set position to middle. Else If array[middle] is greater than the desired value Set last to middle - 1. Else Set first to middle + 1. End If. End While. Return position.

A Binary Search Function int binary. Search(int array[], { int first = 0, last

A Binary Search Function int binary. Search(int array[], { int first = 0, last = size - 1, middle, position = -1; bool found = false; int size, int value) // // // First array element Last array element Mid point of search Position of search value Flag while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle - 1; else first = middle + 1; } return position; } // Calculate mid point // If value is found at mid // If value is in lower half // If value is in upper half

0 1 2 3 4 5 6 10 14 19 27 31 35 42

0 1 2 3 4 5 6 10 14 19 27 31 35 42 log 2 N

Binary Search - Tradeoffs � Benefits: ◦ Much more efficient than linear search. For

Binary Search - Tradeoffs � Benefits: ◦ Much more efficient than linear search. For array of N elements, performs at most log 2 N comparisons � Disadvantages: ◦ Requires that array elements be sorted

Introduction to Sorting Algorithms

Introduction to Sorting Algorithms

Introduction to Sorting Algorithms � Sort: arrange values into an order: ◦ Ascending numeric

Introduction to Sorting Algorithms � Sort: arrange values into an order: ◦ Ascending numeric ◦ Descending numeric � Two algorithms considered here: ◦ Bubble sort ◦ Selection sort

Bubble Sort Algorithm � � � Bubble sort algorithm starts by comparing the first

Bubble Sort Algorithm � � � Bubble sort algorithm starts by comparing the first two elements of an array and swapping if necessary, i. e. , if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element. Then, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result. But, for better performance, in second step, last and second last elements are not compared becuase, the proper element is automatically placed at last after first step. Similarly, in third step, last and second last and third last elements are not compared and so on. A figure is worth a thousand words so, acknowledge this figure for better understanding of bubble sort.

Bubble Sort Algorithm(cont. ) � Here, there are 5 elements to the sorted. -2

Bubble Sort Algorithm(cont. ) � Here, there are 5 elements to the sorted. -2 45 0 11 -9

Bubble Sort Algorithm(cont. ) step=0; step<n-2 i=0; i<n-1 -step

Bubble Sort Algorithm(cont. ) step=0; step<n-2 i=0; i<n-1 -step

Bubble Sort Algorithm(cont. ) /*C Program To Sort data in ascending order using bubble

Bubble Sort Algorithm(cont. ) /*C Program To Sort data in ascending order using bubble sort. */ #include <stdio. h> int main() { int data[100], i, n, step, temp; printf("Enter the number of elements to be sorted: "); scanf("%d", &n); for(i=0; i<n; ++i) { printf("%d. Enter element: ", i+1); scanf("%d", &data[i]); } for(step=0; step<n-2; ++step) for(i=0; i<n-step-1; ++i) { if(data[i]>data[i+1]) /* To sort in descending order, change > to < in this line. */ { temp=data[i]; data[i]=data[i+1]; data[i+1]=temp; } } printf("In ascending order: "); for(i=0; i<n; ++i) printf("%d ", data[i]); return 0; }

Bubble Sort Algorithm(cont. ) Complexity: Order of (n) for the best case Order of

Bubble Sort Algorithm(cont. ) Complexity: Order of (n) for the best case Order of (n 2) for the worst case,

Bubble Sort - Tradeoffs � Benefit: ◦ Easy to understand implement � Disadvantage: ◦

Bubble Sort - Tradeoffs � Benefit: ◦ Easy to understand implement � Disadvantage: ◦ Inefficient: slow for large arrays

Selection Sort list is divided into two sublists, sorted and unsorted, which are divided

Selection Sort list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. � We find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. � After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. � Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. � A list of n elements requires n-1 passes to completely rearrange the data. � The CENG 213 Data Structures

Sorted Unsorted 23 78 45 8 32 56 Original List 8 78 45 23

Sorted Unsorted 23 78 45 8 32 56 Original List 8 78 45 23 32 56 After pass 1 8 23 45 78 32 56 After pass 2 8 23 32 78 45 56 After pass 3 8 23 32 45 78 56 After pass 4 8 23 32 45 56 78 After pass 5 CENG 213 Data Structures

#include <stdio. h> int main() { int index. Min, i, j; int. Array[]={5, 3,

#include <stdio. h> int main() { int index. Min, i, j; int. Array[]={5, 3, 6, 8, 1, 9}; int MAX=6; // loop through all numbers for(i = 0; i < MAX; i++) { // set current element as minimum index. Min = i; // check the element to be minimum for(j = i+1; j<MAX; j++) { if(int. Array[j] < int. Array[index. Min]) { index. Min = j; } } if(index. Min != i) { printf("Items swapped: [ %d, %d ]n" , int. Array[i], int. Array[index. Min]); // swap the numbers int temp = int. Array[index. Min]; int. Array[index. Min] = int. Array[i]; int. Array[i] = temp; } printf("Iteration %d#: n", (i+1)); } for (i=0; i<MAX; i++) printf("%dn", int. Array[i]); }

Selection Sort - Tradeoffs � Benefit: ◦ More efficient than Bubble Sort, since fewer

Selection Sort - Tradeoffs � Benefit: ◦ More efficient than Bubble Sort, since fewer exchanges � Disadvantage: ◦ May not be as easy as Bubble Sort to understand