Download presentation
Presentation is loading. Please wait.
Published byHorace Palmer Modified over 9 years ago
1
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University K.S.A.
2
2 Divide and Conquer CSC 244 Concepts of Algorithms The most well known algorithm design strategy: 1. Divide a large problem into two or more smaller problems 2.Solve smaller problems recursively 3.Obtain solution to original (larger) problem by combining/merging these solutions of small problems.
3
Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n CSC 244 Concepts of Algorithms3
4
Divide and Conquer Examples Searching: binary search Sorting: mergesort Sorting: quicksort Tree traversals Matrix multiplication-Strassen’s algorithm CSC 244 Concepts of Algorithms4
5
Binary Search Recall your mind, we have studied something about Binary Search in Unit-1 & Unit-2. Binary search is an efficient algorithm for searching in a sorted array. Search requires the following steps: 1. Inspect the middle item of an array of size N. 2. Inspect the middle of an array of size N/2. 3. Inspect the middle item of an array of size N/4 and so on until Lower Bound becomes > Upper bound. – This implies k = log 2 N – k is the number of partitions. CSC 244 Concepts of Algorithms5
6
Binary Search Requires that the array be sorted. Rather than start at either end, binary searches split the array in half and works only with the half that may contain the value. This action of dividing continues until the desired value is found or the remaining values are either smaller or larger than the search value. CSC 244 Concepts of Algorithms6
7
Pseudo code for Binary Search BinarySearch(A, START, END, key) { If (START <= END) { MID = (START + END) / 2 [ compute mid point] if (key equal to A[MID] return MID [ found it, Successful Search.] else if ( key < A[MID] ) [ Recursive Call itself for the lower part of the array ] return BinarySearch( START, MID-1, key) else [Recursive Call itself for the upper part of the array ] return BinarySearch(MID+1, END, key) } return -1 [ failed to find key, Un-Successful Search. ] } CSC 244 Concepts of Algorithms7
8
Binary Search Suppose that the data set is n = 2 k – 1 sorted items, where k is the number of partitions. Each time examine middle item. If larger, look left, if smaller look right. Second ‘chunk’ to consider is n/2 in size, third is n/4, fourth is n/8, etc. Worst case, examine k chunks. n = 2 k – 1 so, k = log 2 (n ). (Decision binary Search: one comparison on each level) Best case, O(1). (Found at n/2). Worst Case: O(log 2 (n)) CSC 244 Concepts of Algorithms8
9
57913323342545688 01234567890123456789 Indices Contents Target/Skey is 33 The array a looks like this: Binary Search Example-1 ( Successful Search ) start + End mid = (0 + 9) / 2 (which is 4 ) 33 > A[mid] (that is, 33 > A[4] ) Start = Mid + 1 So, if 33 is in the array, then 33 is one of: 3342545688 56789 56789 Eliminated half of the remaining elements from consideration because array elements are sorted. CSC 244 Concepts of Algorithms9
10
57913323342545688 01234567890123456789 Indices Contents Target/Skey is 33 The array a looks like this: Binary Search Example mid = (5 + 6) / 2 (which is 5 ) 33 == A[mid] So we found 33 at index 5 : 33 5 mid = (5 + 9) / 2 (which is 7 ) 33 < A[mid] (that is, 33 < A[7] ) End= Mid - 1 So, if 33 is in the array, then 33 is one of: 3342 56 56 Eliminate half of the remaining elements CSC 244 Concepts of Algorithms10
11
57913323342545688 01234567890123456789 Indices Contents Target/Skey is 8 The array a looks like this: Binary Search Example-2( Un-Successful Search ) start + End mid = (0 + 9) / 2 (which is 4 ) 8 < A[mid] (that is, 8 < A[4] ) End = Mid - 1 So, if 8 is in the array, then 8 is one of: Eliminated half of the remaining elements from consideration because array elements are sorted. CSC 244 Concepts of Algorithms11 57913 01230123 Indices Contents
12
57913323342545688 01234567890123456789 Indices Contents Target/Skey is 8 The array a looks like this: Binary Search Example mid = (2 + 3) / 2 (which is 2 ) 8 < A[mid] (that is, 8 < A[2] ) change End = Mid - 1 So, now Start = 2 and End = 1, ( that is, Start > End ) which indicates that 8 is not in the array ( Un-successful Search ) mid = (0 + 3) / 2 (which is 1 ) 8 > A[mid] (that is, 8 > A[1] ) change Start = Mid + 1 So, if 8 is in the array, then 8 is one of: 9 13 2 3 Eliminate half of the remaining elements CSC 244 Concepts of Algorithms12
13
CSC 244 Concepts of Algorithms 13
14
14 CSC 244 Concepts of Algorithms
15
C-Language Code for Recursive Binary Search #include int size; // Globle Variable int *A; // integer pointer to store the Base // Address of an integer array void get_Size( ) { cout<<" Put the size of array: ? "; cin>>size; A = new int[size]; // Creation of a // Dynamic Array } void get_numbers( ) { cout<<"\n\n Put "<<size<<“ Numbers : \n"; for( int i = 0 ; i < size ; i++) { cout > A[i];} } 15 CSC 244 Concepts of Algorithms void BubbleSort( ) { int i, pass, hold, sw=1; for (pass=1; pass<= size-1; pass++) { sw=0; for (i=0; i< size-pass; i++) { if(A[i] > A[i+1]) { hold =A[i]; A[i]=A[i+1]; A[i+1]=hold; sw=1; } if(sw==0) break; } void print_sorted_numbers() { cout<<"\n\n\n Sorted numbers are:\n\n"; for ( int i=0; i< size; i++) { cout<<A[i]<<"\t"; } }
16
C-Language Code for Recursive Binary Search int main() { int SKEY, LOC; get_Size(); get_numbers(); BubbleSort(); print_sorted_numbers( ); while(1) { cout<<"\n\n Put Search Key or 0 to stop: "; cin>>SKEY; if (SKEY == 0 ) break; LOC = BinarySearch ( 0, size-1, SKEY ); if(LOC == -1) cout<<" \n "<<SKEY<<" not found"<<endl; else cout<<SKEY<< “ found at Index/location: "<<LOC<<endl; } return 0; } // end of main() function 16 CSC 244 Concepts of Algorithms int BinarySearch(int START, int END, int key) { if (START <= END) { int MID = (START + END) / 2; // compute // mid point. if (key == A[MID]) return MID; // found it, Successful Search. else if (key < A[MID] ) // Call itself for // the lower part of the array return BinarySearch( START, MID-1, key); else // Call itself for the upper part of the array return BinarySearch(MID+1, END, key); } return -1; // Un-Sucessfull Search. }
17
Merge Sort 17 CSC 244 Concepts of Algorithms
18
Divide and Conquer 1.Base case/criteria: the problem is small enough, solve directly 2.Divide the problem into two or more similar and smaller subproblems 3.Recursively solve the subproblems 4.Combine solutions to the subproblems 18 CSC 244 Concepts of Algorithms
19
Divide and Conquer - Sort Problem: Input: A[n] – unsorted array of n ≥1 integers. Output: A[n] – sorted in non-decreasing order 19 CSC 244 Concepts of Algorithms
20
Divide and Conquer - Sort Base case single element (n=1), return Divide A into two subarrays: FirstPart, SecondPart Two Subproblems: sort the FirstPart sort the SecondPart Recursively sort FirstPart sort SecondPart Combine sorted FirstPart and sorted second part 20 CSC 244 Concepts of Algorithms
21
Merging Suppose A is sorted list with r elements and B is a sorted list with s elements. The operation that combine the elements of A and B into a single sorted list C with n= r+s elements is called merging. One simple way to merge is to place the elements of B after the elements of A and then use some sorting algorithm on the entire lsit. This method does not take advantage of the fact that A and B are individually sorted. A much more efficient algorithm is merge sort algorithm. Suppose one is given two sorted decks of cards. The decks are merged as: 21 CSC 244 Concepts of Algorithms
22
Merging (cont.) 3102354 152575 X:Y: Result: 22 CSC 244 Concepts of Algorithms
23
Merging (cont.) 3102354 52575 1 X:Y: Result: 23 CSC 244 Concepts of Algorithms
24
Merging (cont.) 102354 52575 13 X:Y: Result: 24 CSC 244 Concepts of Algorithms
25
Merging (cont.) 102354 2575 135 X:Y: Result: 25 CSC 244 Concepts of Algorithms
26
Merging (cont.) 2354 2575 13510 X:Y: Result: 26 CSC 244 Concepts of Algorithms
27
Merging (cont.) 54 2575 1351023 X:Y: Result: 27 CSC 244 Concepts of Algorithms
28
Merging (cont.) 54 75 135102325 X:Y: Result: 28 CSC 244 Concepts of Algorithms
29
Merging (cont.) 75 13510232554 X:Y: Result: 29 CSC 244 Concepts of Algorithms
30
Merging (cont.) 1351023255475 X:Y: Result: 30 CSC 244 Concepts of Algorithms
31
Merge Sort Example 996861558358640 31 CSC 244 Concepts of Algorithms
32
Merge Sort Example 996861558358640 996861558358640 32 CSC 244 Concepts of Algorithms
33
Merge Sort Example 996861558358640 996861558358640 1599658358640 33 CSC 244 Concepts of Algorithms
34
Merge Sort Example 996861558358640 996861558358640 1599658358640 996861558358640 34 CSC 244 Concepts of Algorithms
35
Merge Sort Example 996861558358640 996861558358640 1599658358640 996861558358640 40 35 CSC 244 Concepts of Algorithms
36
Merge Sort Example 996861558358604 40 36 CSC 244 Concepts of Algorithms
37
Merge Sort Example 158669935580486 996861558358604 37 CSC 244 Concepts of Algorithms
38
Merge Sort Example 615869904355886 158669935580486 38 CSC 244 Concepts of Algorithms
39
Merge Sort Example 04615355886 99 615869904355886 39 CSC 244 Concepts of Algorithms
40
Merge Sort Example 04615355886 99 40 CSC 244 Concepts of Algorithms
41
Another Example MergeSort 41 CSC 244 Concepts of Algorithms
42
Merge Sort: Algorithm Algorithm: MERGESORT (A, N) 1- If N=1, Return. 2- Set N1=N/2, N2=N-N1. 3- Repeat for i=0,1,2..... (N1-1) Set L [i]=A [i]. 4- Repeat for j=0,1,2..... (N2-1) Set R [j]=A [N1+j]. 5- Call MERGESORT (L, N1). 6 - Call MERGESORT (R, N2). 7- Call MERGE (A, L, N1, R,N2). 8- Return. 42 CSC 244 Concepts of Algorithms
43
Merge : Algorithm Algorithm: MERGE (A, L, N1, R, N2) 1- Set i=0, j:=0. 2- Repeat for k=0,1,2..... (N1+N2-1) If i<N1, then: If j=N2 or L [i] ≤ R [j], then: Set A [k] =L [i]. Set i=i+1; Else: If j < N2, then: Set A [k]=R [j]. Set j=j+1. 3- Return. 43 CSC 244 Concepts of Algorithms
44
Merge Sort: Algorithm Merge-Sort (A, n) if n=1 return else n 1 = n/2 and n 2 = n – n 1 create array L[n 1 ], R[n 2 ] for i = 0 to n 1 -1 do L[i] ← A[i] for j = 0 to n 2 -1 do R[j] ← A[n 1 +j] Merge-Sort(L, n 1 ) Merge-Sort(R, n 2 ) Merge(A, L, n 1, R, n 2 ) Space: n Recursive Call Time: n 44 CSC 244 Concepts of Algorithms
45
merge(A,L,n 1,R,n 2 ) i = j = 0 for k = 0 to n 1 +n 2 -1 if i < n 1 if j = n 2 or L[i] ≤ R[j] A[k] = L[i] i = i + 1 else if j < n 2 A[k] = R[j] j = j + 1 Number of iterations: (n 1 +n 2 ) Total time: c(n 1 +n 2 ) for some c Merge Sort: Algorithm CSC 244 Concepts of Algorithms 45
46
Home Work Using the algorithm described in above slides, write a c-language code for MERGE-SORT 46 CSC 244 Concepts of Algorithms
47
END 47 CSC 244 Concepts of Algorithms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.