Please go through the previous DS Notes if you donβt have the notes. To get the complexity tables and graphs check here.
Divide and Conquer Method
Application:
- Maximum subarray Problem
- Binary Search
- Merge Sort
- Quick Sort
- Strassen's Matrix multiplication
- Largest sub-array sum
- Finding the closest pair problem
- Convex Hull problem
Maximum Subarray Sum Problem
Given an array of n numbers, find the (a) contiguous sub-array whose sum has the largest value.
To know about the concepts, watch this video.
// Divide and Conquer Method #include<cmath> #include<iostream> #include<climits> using namespace std; int Max_Subarray_Sum(int arr[],int n) { if(n==1) { return arr[0]; } int m = n/2; int left_MSS = Max_Subarray_Sum(arr,m); int right_MSS = Max_Subarray_Sum(arr+m,n-m); int leftsum = INT_MIN, rightsum = INT_MIN, sum=0; for(int i=m;i < n; i++) { sum += arr[i]; rightsum = max(rightsum,sum); } sum = 0; for(int i= (m-1);i >= 0; i--) { sum += arr[i]; leftsum = max(leftsum,sum); } int ans = max(left_MSS,right_MSS); return max(ans,leftsum+rightsum); } int main(int argc, char const *argv[]) { int arr[] = {3,-2,5,-1}; cout<<Max_Subarray_Sum(arr,4)<<"\n"; return 0; }
Time Complexity: MaxSubArraySum() is a recursive method and time complexity can be expressed as following recurrence relation. T(n) = 2T(n/2) + Ξ(n)
Binary Search
For binary search code and complexity click here.
Merge Sort
To understand the concepts go here.
// Merge sort in C++ #include <iostream> using namespace std; // Merge two subarrays L and M into arr void merge(int arr[], int p, int q, int r) { // Create L β A[p..q] and M β A[q+1..r] int n1 = q - p + 1; int n2 = r - q; int L[n1], M[n2]; for (int i = 0; i < n1; i++) L[i] = arr[p + i]; for (int j = 0; j < n2; j++) M[j] = arr[q + 1 + j]; // Maintain current index of sub-arrays and main array int i, j, k; i = 0; j = 0; k = p; // Until we reach either end of either L or M, pick larger among // elements L and M and place them in the correct position at A[p..r] while (i < n1 && j < n2) { if (L[i] <= M[j]) { arr[k] = L[i]; i++; } else { arr[k] = M[j]; j++; } k++; } // When we run out of elements in either L or M, // pick up the remaining elements and put in A[p..r] while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = M[j]; j++; k++; } } // Divide the array into two subarrays, sort them and merge them void mergeSort(int arr[], int l, int r) { if (l < r) { // m is the point where the array is divided into two subarrays int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); // Merge the sorted subarrays merge(arr, l, m, r); } } // Print the array void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } // Driver program int main() { int arr[] = {6, 5, 12, 10, 9, 1}; int size = sizeof(arr) / sizeof(arr[0]); mergeSort(arr, 0, size - 1); cout << "Sorted array: \n"; printArray(arr, size); return 0; }
Time Complexity O(n*log n) Space complexity O(n)
Quick Sort
Learn the concepts here.
// Quick sort in C++ #include <iostream> using namespace std; // function to swap elements void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } // function to print the array void printArray(int array[], int size) { int i; for (i = 0; i < size; i++) cout << array[i] << " "; cout << endl; } // function to rearrange array (find the partition point) int partition(int array[], int low, int high) { // select the rightmost element as pivot int pivot = array[high]; // pointer for greater element int i = (low - 1); // traverse each element of the array // compare them with the pivot for (int j = low; j < high; j++) { if (array[j] <= pivot) { // if element smaller than pivot is found // swap it with the greater element pointed by i i++; // swap element at i with element at j swap(&array[i], &array[j]); } } // swap pivot with the greater element at i swap(&array[i + 1], &array[high]); // return the partition point return (i + 1); } void quickSort(int array[], int low, int high) { if (low < high) { // find the pivot element such that // elements smaller than pivot are on left of pivot // elements greater than pivot are on righ of pivot int pi = partition(array, low, high); // recursive call on the left of pivot quickSort(array, low, pi - 1); // recursive call on the right of pivot quickSort(array, pi + 1, high); } } // Driver code int main() { int data[] = {8, 7, 6, 1, 0, 9, 2}; int n = sizeof(data) / sizeof(data[0]); cout << "Unsorted Array: \n"; printArray(data, n); // perform quicksort on data quickSort(data, 0, n - 1); cout << "Sorted array in ascending order: \n"; printArray(data, n); }
Time Complexity O(n*log n) and space complexity O(log n)
Β