From 3ebdf03a4de493d99934cbc5e671c522e04d55b1 Mon Sep 17 00:00:00 2001 From: Abhiram Rajeevan <54032786+AbhiramRajeevan@users.noreply.github.com> Date: Thu, 14 Oct 2021 17:03:53 +0530 Subject: [PATCH 1/3] Add mergesort --- C/MergeSort.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 C/MergeSort.c diff --git a/C/MergeSort.c b/C/MergeSort.c new file mode 100644 index 0000000..6fcd193 --- /dev/null +++ b/C/MergeSort.c @@ -0,0 +1,90 @@ +#include +using namespace std; + +// Merges two subarrays of array[]. +// First subarray is arr[begin..mid] +// Second subarray is arr[mid+1..end] +void merge(int array[], int const left, int const mid, int const right) +{ + auto const subArrayOne = mid - left + 1; + auto const subArrayTwo = right - mid; + + // Create temp arrays + auto *leftArray = new int[subArrayOne], + *rightArray = new int[subArrayTwo]; + + // Copy data to temp arrays leftArray[] and rightArray[] + for (auto i = 0; i < subArrayOne; i++) + leftArray[i] = array[left + i]; + for (auto j = 0; j < subArrayTwo; j++) + rightArray[j] = array[mid + 1 + j]; + + auto indexOfSubArrayOne = 0, // Initial index of first sub-array + indexOfSubArrayTwo = 0; // Initial index of second sub-array + int indexOfMergedArray = left; // Initial index of merged array + + // Merge the temp arrays back into array[left..right] + while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { + if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) { + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + } + else { + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + } + indexOfMergedArray++; + } + // Copy the remaining elements of + // left[], if there are any + while (indexOfSubArrayOne < subArrayOne) { + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + indexOfMergedArray++; + } + // Copy the remaining elements of + // right[], if there are any + while (indexOfSubArrayTwo < subArrayTwo) { + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + indexOfMergedArray++; + } +} + +// begin is for left index and end is +// right index of the sub-array +// of arr to be sorted */ +void mergeSort(int array[], int const begin, int const end) +{ + if (begin >= end) + return; // Returns recursively + + auto mid = begin + (end - begin) / 2; + mergeSort(array, begin, mid); + mergeSort(array, mid + 1, end); + merge(array, begin, mid, end); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (auto i = 0; i < size; i++) + cout << A[i] << " "; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + auto arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Given array is \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "\nSorted array is \n"; + printArray(arr, arr_size); + return 0; +} From 01504cd92559695877e3503a3b82ad56064f1f3e Mon Sep 17 00:00:00 2001 From: Abhiram Rajeevan <54032786+AbhiramRajeevan@users.noreply.github.com> Date: Thu, 14 Oct 2021 17:17:37 +0530 Subject: [PATCH 2/3] Add mergesort.c --- C/MergeSort.c | 122 ++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 58 deletions(-) diff --git a/C/MergeSort.c b/C/MergeSort.c index 6fcd193..249112b 100644 --- a/C/MergeSort.c +++ b/C/MergeSort.c @@ -1,90 +1,96 @@ -#include -using namespace std; +#include +#include -// Merges two subarrays of array[]. -// First subarray is arr[begin..mid] -// Second subarray is arr[mid+1..end] -void merge(int array[], int const left, int const mid, int const right) +// 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) { - auto const subArrayOne = mid - left + 1; - auto const subArrayTwo = right - mid; + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; - // Create temp arrays - auto *leftArray = new int[subArrayOne], - *rightArray = new int[subArrayTwo]; + /* create temp arrays */ + int L[n1], R[n2]; - // Copy data to temp arrays leftArray[] and rightArray[] - for (auto i = 0; i < subArrayOne; i++) - leftArray[i] = array[left + i]; - for (auto j = 0; j < subArrayTwo; j++) - rightArray[j] = array[mid + 1 + j]; + /* 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]; - auto indexOfSubArrayOne = 0, // Initial index of first sub-array - indexOfSubArrayTwo = 0; // Initial index of second sub-array - int indexOfMergedArray = left; // Initial index of merged array - - // Merge the temp arrays back into array[left..right] - while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { - if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) { - array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; - indexOfSubArrayOne++; + /* 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 { - array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; - indexOfSubArrayTwo++; + arr[k] = R[j]; + j++; } - indexOfMergedArray++; + k++; } - // Copy the remaining elements of - // left[], if there are any - while (indexOfSubArrayOne < subArrayOne) { - array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; - indexOfSubArrayOne++; - indexOfMergedArray++; + + /* Copy the remaining elements of L[], if there + are any */ + while (i < n1) { + arr[k] = L[i]; + i++; + k++; } - // Copy the remaining elements of - // right[], if there are any - while (indexOfSubArrayTwo < subArrayTwo) { - array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; - indexOfSubArrayTwo++; - indexOfMergedArray++; + + /* Copy the remaining elements of R[], if there + are any */ + while (j < n2) { + arr[k] = R[j]; + j++; + k++; } } -// begin is for left index and end is -// right index of the sub-array -// of arr to be sorted */ -void mergeSort(int array[], int const begin, int const end) +/* l is for left index and r is right index of the +sub-array of arr to be sorted */ +void mergeSort(int arr[], int l, int r) { - if (begin >= end) - return; // Returns recursively + if (l < r) { + // Same as (l+r)/2, but avoids overflow for + // large l and h + int m = l + (r - l) / 2; - auto mid = begin + (end - begin) / 2; - mergeSort(array, begin, mid); - mergeSort(array, mid + 1, end); - merge(array, begin, mid, end); + // Sort first and second halves + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } } -// UTILITY FUNCTIONS -// Function to print an array +/* UTILITY FUNCTIONS */ +/* Function to print an array */ void printArray(int A[], int size) { - for (auto i = 0; i < size; i++) - cout << A[i] << " "; + int i; + for (i = 0; i < size; i++) + printf("%d ", A[i]); + printf("\n"); } -// Driver code +/* Driver code */ int main() { int arr[] = { 12, 11, 13, 5, 6, 7 }; - auto arr_size = sizeof(arr) / sizeof(arr[0]); + int arr_size = sizeof(arr) / sizeof(arr[0]); - cout << "Given array is \n"; + printf("Given array is \n"); printArray(arr, arr_size); mergeSort(arr, 0, arr_size - 1); - cout << "\nSorted array is \n"; + printf("\nSorted array is \n"); printArray(arr, arr_size); return 0; } From 85dca905bb68e9fca7693b7552f303e4aa41fd5a Mon Sep 17 00:00:00 2001 From: Abhiram Rajeevan <54032786+AbhiramRajeevan@users.noreply.github.com> Date: Thu, 14 Oct 2021 17:23:52 +0530 Subject: [PATCH 3/3] Create Quicksort.c --- C/QuickSort.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 C/QuickSort.c diff --git a/C/QuickSort.c b/C/QuickSort.c new file mode 100644 index 0000000..1a7d75f --- /dev/null +++ b/C/QuickSort.c @@ -0,0 +1,78 @@ +#include + +// function to swap elements +void swap(int *a, int *b) { + int t = *a; + *a = *b; + *b = t; +} + +// function to find the partition position +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 the pivot element 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 right 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); + } +} + +// function to print array elements +void printArray(int array[], int size) { + for (int i = 0; i < size; ++i) { + printf("%d ", array[i]); + } + printf("\n"); +} + +// main function +int main() { + int data[] = {8, 7, 2, 1, 0, 9, 6}; + + int n = sizeof(data) / sizeof(data[0]); + + printf("Unsorted Array\n"); + printArray(data, n); + + // perform quicksort on data + quickSort(data, 0, n - 1); + + printf("Sorted array in ascending order: \n"); + printArray(data, n); +}