From 7d8a3621a585fbd1bf1decfbfe2e6af5ef81eb26 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 13 Apr 2020 22:16:33 +0530 Subject: [PATCH] Quick sort and merge sort added --- Sorting Algorithms/merge-sort.cpp | 102 ++++++++++++++++++++++++++++++ Sorting Algorithms/quick-sort.cpp | 72 +++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 Sorting Algorithms/merge-sort.cpp create mode 100644 Sorting Algorithms/quick-sort.cpp diff --git a/Sorting Algorithms/merge-sort.cpp b/Sorting Algorithms/merge-sort.cpp new file mode 100644 index 0000000..40c304d --- /dev/null +++ b/Sorting Algorithms/merge-sort.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +// Merges two subarrays of arr[]. +// First subarray is arr[l..m] +// Second subarray is arr[m+1..r] +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + /* create temp arrays */ + int L[n1], R[n2]; + + /* Copy data to temp arrays L[] and R[] */ + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1+ j]; + + /* Merge the temp arrays back into arr[l..r]*/ + i = 0; // Initial index of first subarray + j = 0; // Initial index of second subarray + k = l; // Initial index of merged subarray + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} + + +void mergeSort(int arr[], int l, int r) +{ + if (l < r) + { + int m = l+(r-l)/2; + mergeSort(arr, l, m); + mergeSort(arr, m+1, r); + + merge(arr, l, m, r); + } +} + + +void printArray(int A[], int size) +{ + int i; + for (i=0; i < size; i++) + cout<>n; + cout<<"Enter the elements: "; + + for(int i=0;i>arr[i]; + } + + + + cout<<"Given array is: "< +using namespace std; + + +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + + +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element + + for (int j = low; j <= high - 1; j++) + { + // If current element is smaller than the pivot + if (arr[j] < pivot) + { + i++; // increment index of smaller element + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + + +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + + int pi = partition(arr, low, high); + + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + + +void display(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + + +int main() +{ + int arr[10]; + int n; + cout<<"Enter the size of array: "; + cin>>n; + cout<<"Enter the elements of array: "; + for(int i=0;i>arr[i]; + } + cout<<"Enter the index of element you want to pivot: "; + int pivot; + cin>>pivot; + swap(&arr[pivot],&arr[n-1]); + quickSort(arr, 0, n - 1); + cout << "Sorted array: \n"; + display(arr, n); + return 0; +}