From 855b3dda9023a6a7134d47666743a0e375da1ea8 Mon Sep 17 00:00:00 2001 From: Rishabh Bansal Date: Mon, 15 Oct 2018 22:28:06 +0530 Subject: [PATCH] Create quicksort.c --- sorting/Python/quicksort.c | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 sorting/Python/quicksort.c diff --git a/sorting/Python/quicksort.c b/sorting/Python/quicksort.c new file mode 100644 index 0000000..ba8c595 --- /dev/null +++ b/sorting/Python/quicksort.c @@ -0,0 +1,45 @@ +#include +#include + +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 (arr[j] <= pivot) + { + i++; + 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 printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + cout<