Download presentation
Presentation is loading. Please wait.
1
Introduction to Search Algorithms
2
Introduction to Search Algorithms
Search: locate an item in a list of information Two algorithms we will examine: Linear search Binary search
3
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.
4
Linear Search - Example
Array numlist contains: Searching for the the value 11, linear search examines 17, 23, 5, and 11 Searching for the the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3 17 23 5 11 2 29 3
5
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
6
A Linear Search Function
#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); if(A[i]==key) indx=i; break; if(indx==-1) printf("the key does not exist"); else printf("the key does exist at index: %d ",indx); return 0; A Linear Search Function
7
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
8
Binary Search 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
9
Binary Search - Example
Array numlist2 contains: Searching for the the value 11, binary search examines 11 and stops Searching for the the value 7, linear search examines 11, 3, 5, and stops 2 3 5 11 17 23 29
10
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.
11
A Binary Search Function
int binarySearch(int array[], int size, int value) { int first = 0, // First array element last = size - 1, // Last array element middle, // Mid point of search position = -1; // Position of search value bool found = false; // Flag while (!found && first <= last) { middle = (first + last) / 2; // Calculate mid point if (array[middle] == value) // If value is found at mid { found = true; position = middle; } else if (array[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; }
12
1 2 3 4 5 6 10 14 19 27 31 35 42 log2N
13
Binary Search - Tradeoffs
Benefits: Much more efficient than linear search. For array of N elements, performs at most log2N comparisons Disadvantages: Requires that array elements be sorted
14
Introduction to Sorting Algorithms
15
Introduction to Sorting Algorithms
Sort: arrange values into an order: Ascending numeric Descending numeric Two algorithms considered here: Bubble sort Selection sort
16
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 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.
17
Bubble Sort Algorithm(cont.)
Here, there are 5 elements to the sorted. -2 45 11 -9
18
Bubble Sort Algorithm(cont.)
step=0;step<n-2 i=0;i<n-1-step
19
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: "); printf("%d ",data[i]); return 0;
20
Bubble Sort Algorithm(cont.)
Complexity: Order of (n) for the best case Order of (n2) for the worst case,
21
Bubble Sort - Tradeoffs
Benefit: Easy to understand and implement Disadvantage: Inefficient: slow for large arrays
22
Selection Sort The 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. CENG 213 Data Structures
23
Sorted Unsorted 23 78 45 8 32 56 Original List After pass 1
CENG 213 Data Structures
24
#include <stdio.h>
int main() { int indexMin,i,j; int intArray[]={5,3,6,8,1,9}; int MAX=6; // loop through all numbers for(i = 0; i < MAX; i++) { // set current element as minimum indexMin = i; // check the element to be minimum for(j = i+1; j<MAX; j++) { if(intArray[j] < intArray[indexMin]) { indexMin = j; } if(indexMin != i) { printf("Items swapped: [ %d, %d ]\n" , intArray[i], intArray[indexMin]); // swap the numbers int temp = intArray[indexMin]; intArray[indexMin] = intArray[i]; intArray[i] = temp; printf("Iteration %d#:\n",(i+1)); for (i=0;i<MAX;i++) printf("%d\n",intArray[i]);
25
Selection Sort - Tradeoffs
Benefit: More efficient than Bubble Sort, since fewer exchanges Disadvantage: May not be as easy as Bubble Sort to understand
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.