The document discusses searching and sorting algorithms using the divide and conquer approach. It describes linear search and binary search algorithms. Linear search has a time complexity of O(n) as it checks each element sequentially. Binary search has a time complexity of O(log n) as it divides the search space in half each iteration by comparing the target to the middle element. It requires the list to be sorted, unlike linear search which can be used on unsorted lists.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views
Chapter Two
The document discusses searching and sorting algorithms using the divide and conquer approach. It describes linear search and binary search algorithms. Linear search has a time complexity of O(n) as it checks each element sequentially. Binary search has a time complexity of O(log n) as it divides the search space in half each iteration by comparing the target to the middle element. It requires the list to be sorted, unlike linear search which can be used on unsorted lists.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26
Chapter two
Searching and sorting algorithms:
divide and conquer approach Divide and conquer • it is the process of dividing a complex problem into two or more sub- problems of the same type until the sub-problems are easy enough to be solved directly and recursively, • and then these solutions are combined to give a solution to the initial problem. • It is the foundation of efficient algorithms for many different problems, • including sorting problems and the discrete Fourier transform. • main steps of the divide and conquer paradigm 1. Divide the problem into two (or more) sub-problems 2. Conquer these sub problems recursively 3. Combine the solutions to the sub problems in order to get a solution to the original problem. Advantages of divide and conquer paradigm • It allows us to solve difficult and often impossible looking problems, such as the Tower of Hanoi • it reduces the degree of difficulty since it divides the problem into sub problems • Runs faster than other algorithms would • it often plays a part in finding other efficient algorithms, and in fact it was the central role in finding the quick sort and merge sort algorithms. • It also uses memory caches effectively. • when the sub problems become simple enough, they can be solved within a cache, without having to access the slower main memory Disadvantages • The recursion is slow • It can become more complicated than a basic iterative approach, • especially in cases with a large n (add a large amount of numbers together) • sometimes once the problem is broken down into sub problems, the same sub problem can occur many times. • these algorithms can be carried out by a non-recursive program that will store the different sub problems in things called explicit stacks (not call stacks), • which gives more freedom in deciding just which order the sub problems should be solved. Some algorithms • Binary Search, • the Merge Sort Algorithm, • the Quick sort algorithm, • Matrix Multiplication, • and the fast Fourier transform Searching algorithms: linear search • Searching is the process of finding some particular element in the list. • If the element is present in the list, • then the process is called successful and the process returns the location of that element, • otherwise the search is called unsuccessful. • There are two popular search methods that are widely used • Linear Search / Sequential Search • Binary search Linear Search / Sequential Search • Linear search, also called as sequential search • is a very simple method used for searching an array for a particular value. • It works by comparing the value to be searched with every element of the array one by one • mostly used to search an unordered list of elements • int A[ ] = {10, 8, 1, 21, 7, 32, 5, 11, 0}; LINEAR_SEARCH(A, N, VAL) Write a program to search an element in an array using the linear search technique. Step 1: [INITIALIZE] SET POS = -1 Step 2: [INITIALIZE] SET I = 0 #include<stdio.h> Step 3: Repeat Step 4 void main () while { int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9}; I<=N int item, i,flag; Step 4: IF A[I] = VAL printf("\nEnter Item which is to be searched\n"); SET POS = I scanf("%d",&item); PRINT POS for (i = 0; i< 10; i++){ if(a[i] == item){ Go to Step 6 flag = i+1; [END OF IF] break;} SET I=I+1 else [END OF LOOP] flag = 0;}//end of loop if(flag != 0){ Step 5: IF POS = –1 printf("\nItem found at location %d\n",flag); PRINT "VALUE IS NOT PRESENTIN } THE ARRAY" else{ [END OF IF] printf("\nItem not found\n");} } //end of main method Step 6: EXIT Advantages of a linear search • Will perform fast searches of small to medium lists. • With today's powerful computers, small to medium arrays can be searched relatively quickly. • The list does not need to be sorted. • Unlike a binary search, linear searching does not require an ordered list. • Not affected by insertions and deletions. • As the linear search does not require the list to be sorted, additional elements can be added and deleted. • As other searching algorithms may have to reorder the list after insertions or deletions, this may sometimes mean a linear search will be more efficient. Disadvantages of a linear search • Slow searching of large lists. • For example, when searching through a database of everyone in Ethiopia to find a particular name, • it might be necessary to search through 120 million names before you found the one you wanted. • This speed disadvantage is why other search methods have been developed. • Space and time complexity • Space complexity • It takes up no extra space; • its space complexity is O(n) for an array of n elements. Complexity of Linear Search Algorithm • Best Case Complexity • The element being searched could be found in the first position. • In this case, the search ends with a single successful comparison. • Thus, in the best-case scenario, the linear search algorithm performs O(1) operations. • Worst Case Complexity • The element being searched may be at the last position in the array or not at all. • The search succeeds in ‘n’ comparisons or the search fails after ‘n’ comparisons. • Thus, the linear search algorithm performs O(n) operations. • Average Case Complexity • When the element to be searched is in the middle of the array, the average case of the Linear Search Algorithm is O(n). BINARY SEARCH • works efficiently on the sorted lists • follows divide and conquer approach • the list is divided into two halves and the item is compared with the middle element of the list. • If the match is found then, • the location of middle element is returned otherwise, • we search into either of the halves depending upon the result produced through the match. • Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array. • Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array. In 1st step : therefore therefore 1. BEG = 0 in Second step: in third step: 2. END = 8 1. Beg = mid +1 = 5 1. beg = mid + 1 = 7 3. MID = 4 2. End = 8 2. End = 8 4. a[mid] = a[4] = 13 < 23, 3. mid = 13/2 = 6 3. mid = 15/2 = 7 4. a[mid] = a[6] = 20 < 23, 4. a[mid] = a[7] 5. a[7] = 23 = item; 6. therefore, set location = mid; 7. The location of the item will be 7. BINARY_SEARCH(A,lower_bound, upper_bound, VAL) int binarySearch(int a[], int beg, int end, Step 1: [INITIALIZE] SET BEG = lower_bound END = upper_bound, POS = - 1 int item){ Step 2: Repeat Steps 3 and 4 while BEG <= END int mid; Step 3: SET MID = (BEG + END)/2 Step 4: IF A[MID] = VAL if(end >= beg){ SET POS = MID mid = (beg + end)/2; PRINT POS Go to Step 6 if(a[mid] == item){ ELSE IF A[MID] > VAL return mid+1;} SET END = MID - 1 ELSE else if(a[mid] < item){ SET BEG = MID + 1 [END OF IF] return binarySearch(a,mid+1,end,item); [END OF LOOP] }else{ Step 5: IF POS = -1 PRINT “VALUE IS NOT PRESENT IN THE ARRAY” return binarySearch(a,beg,mid-1,item); [END OF IF] }} Step 6: EXIT return -1; Advantages • It eliminates half of the list from further searching by using the result of each comparison. • It indicates whether the element being searched is before or after the current position in the list. • This information is used to narrow the search. • For large lists of data, it works significantly better than linear search. Disadvantages • It employs recursive approach which requires more stack space. • Programming binary search algorithm is error prone and difficult. • The interaction of binary search with memory hierarchy i.e. caching is poor. (because of its random access nature) • Complexity of Binary Search Algorithm • As it disposes off one part of the search case during every step of binary search, and perform the search operation on the other half, • this results in a worst case time complexity of O(logN). • What is the best case and average case time complexity ? Sequential Search Binary Search Time complexity is O(n) Time complexity is O(log n) Finds the key present at first position in Finds the key present at center position in constant time constant time Sequence of elements in the container does The elements must be sorted in the not affect. Container Arrays and linked lists can be used to It cannot be implemented directly into the implement this linked list. We need to change the basic rules of the list to implement this Algorithm is iterative in nature Algorithm technique is Divide and Conquer. Algorithm is easy to implement, and Algorithm is slightly complex. It takes requires less amount of code. more amount of code to implement. N number of comparisons are required for Log n number of comparisons are worst case. sufficient in worst case. Sorting Algorithms: Merge-sort • It is one of the well-known divide-and-conquer algorithms • is a simple and very efficient sorting algorithm. • Steps • Divide: Split Array A into two sub-sequences, each of size roughly n/2 . • Conquer: Sort each subsequence (by calling MergeSort recursively on each). • Combine: Merge the two sorted sub-sequences into a single sorted list. • The dividing process ends when we have split the sub-sequences down to a single item. • It works top-down splitting up the list into smaller sub-lists • The “conquer and combine” phase works bottom-up, merging sorted lists together into larger sorted lists. • Algorithm: 1. Divide the array in to two halves. 2. Recursively sort the first n/2 items. 3. Recursively sort the last n/2 items. 4. Merge sorted items (using an auxiliary array). Division phase Sorting and merging phase Merge(L,R, A) Merge_sort(A) { While(i<nL) { nLlength(L) { Nlength(A) A[k]L[i] If(N<2) nRlength(R) Return ijk0 Ii+1 Midn/2 while(i<nL && j<nR){ Kk+1 Leftarray of size(mid) If(L[i]<=R[j]) { } Rightarray of size(n-mid) A[k]L[i] While(j<nR) For i0 to mid-1 ii+1 { Left[i]A[i] } A[k]R[j] For imid to n-1 Else{ jj+1 Right[i-mid]A[i] A[k]R[j] Kk+1 Merge_sort(left) Jj+1 } Merge_sort(Right) } } Merge(left,right,A) } Kk+1 } Advantages and disadvantages • Advantages : • is best case for sorting slow-access data e.g) tape drive. • is better at handling sequential - accessed lists. • Disadvantages: • Slower comparative to the other sort algorithms for smaller tasks. • Merge sort algorithm requires additional memory space of 0(n) for the temporary array . • It goes through the whole process even if the array is sorted. • Complexity of Merge Sort • The running time of merge sort in the average case and the worst case can be given as O(n logn). • Although merge sort has an optimal time complexity, it needs an additional space of O(n) for the temporary array TEMP. • Applications • Merge Sort is useful for sorting linked lists in O(nLogn) time. • Inversion Count Problem • Used in External Sorting Questions ?