From 0c263bc522ef4b877408f01a21940e9efae4e8f7 Mon Sep 17 00:00:00 2001 From: ayusha Date: Thu, 31 Oct 2019 13:10:38 -0230 Subject: [PATCH 1/2] Add an implementation of quick sort --- Sorting/quick.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Sorting/quick.c diff --git a/Sorting/quick.c b/Sorting/quick.c new file mode 100644 index 0000000..40ad45d --- /dev/null +++ b/Sorting/quick.c @@ -0,0 +1,78 @@ + +#include + +// A utility function to swap two elements +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places + the pivot element at its correct position in sorted + array, and places all smaller (smaller than pivot) + to left of pivot and all greater elements to right + of pivot */ +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); +} + +/* The main function that implements QuickSort + arr[] --> Array to be sorted, + low --> Starting index, + high --> Ending index */ +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + /* pi is partitioning index, arr[p] is now + at right place */ + int pi = partition(arr, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + +} + +// Driver program to test above functions +int main() +{ int size; + printf("enter the size of array"); + scanf("%d",&size); + int arr[size]; + for(int i=0;i Date: Thu, 31 Oct 2019 13:16:17 -0230 Subject: [PATCH 2/2] add an implementation of decimal to octal --- Sorting/decimaltooctal.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Sorting/decimaltooctal.c diff --git a/Sorting/decimaltooctal.c b/Sorting/decimaltooctal.c new file mode 100644 index 0000000..33170e9 --- /dev/null +++ b/Sorting/decimaltooctal.c @@ -0,0 +1,23 @@ + +#include +#include +int convertDecimalToOctal(int decimalNumber); +int main() +{ + int decimalNumber; + printf("Enter a decimal number: "); + scanf("%d", &decimalNumber); + printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber)); + return 0; +} +int convertDecimalToOctal(int decimalNumber) +{ + int octalNumber = 0, i = 1; + while (decimalNumber != 0) + { + octalNumber += (decimalNumber % 8) * i; + decimalNumber /= 8; + i *= 10; + } + return octalNumber; +}