Quick Sort (Divide and Conquer Technique) - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Quick Sort (Divide and Conquer Technique)

Share This
Source code for Quick Sort
#include<stdio.h> #include<stdlib.h> #include<math.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int partition(int *arr, int p, int q) { int i = p - 1, j; int x = arr[q]; for(j = p; j < q; j++){ if(arr[j] <= x){ i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[q]); return i + 1; } void quickSort(int *arr, int p, int q){ if(p < q){ int m = partition(arr, p, q); quickSort(arr, p, m - 1); quickSort(arr, m + 1, q); } } int main() { int *arr, n, i; printf("Enter size of array:"); scanf("%d", &n); printf("Enter items of the array:"); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } quickSort(arr, 0, n-1); for(i = 0; i < n; i++) { printf("%d", arr[i]); } return 0; }

Happy Exploring!

No comments:

Post a Comment