Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void swap (int *x, int *y){
- if (x != y){
- *x += *y; *y = *x - *y; *x -= *y;
- }
- }
- int partition(int a[], int start, int end){
- int pivotIndex = (start+end)/2; //can be anything between start and end.
- int pivotValue = a[pivotIndex], i = start - 1;
- swap(&a[pivotIndex], &a[end]);
- for(int j = start; j != end; j++)
- if(a[j] <= pivotValue) swap(&a[++i], &a[j]);
- swap(&a[i+1], &a[end]);
- return i+1;
- }
- void quickSort(int a[], int start, int end){
- if(start < end){
- int q = partition(a, start, end);
- quickSort(a, start, q - 1);
- quickSort(a, q + 1, end);
- }
- }
- int main(){
- int *a, i, n;
- printf("Enter the number of elements: ");
- scanf("%d", &n);
- a = (int *) malloc(sizeof(int) * n);
- for(i = 0; i < n; i++) scanf("%d", &a[i]);
- quickSort(a, 0, n-1);
- for(i = 0; i < n; i++) printf("\t%d\t", a[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement