Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include #include #include #include

<stdio.h> <math.h> <stdlib.h> <time.h>

int temporary = 0; void swap (int * x, int * y) { temporary = *x; *x = *y; *y = temporary; } void quicksort(int * array, int left, int right, int pivotIndex) { int i; int pivot = array[pivotIndex]; swap(array + pivotIndex, array + right); int leftIndex = left; for (i = left; i< right; i++) { if (array[i]<pivot) { swap(array + leftIndex, array + i); leftIndex++; } } swap (array + right, array + leftIndex); if (leftIndex - left > 2 ) quicksort(array, left, leftIndex - 1, (left + leftIndex-1)/2); else if (leftIndex - left > 1) { if (*(array + left) > *(array + leftIndex -1)) swap(array + left, array + leftIndex - 1); } if ( 2 < right - leftIndex) quicksort(array, leftIndex + 1, right, (leftIndex + 1 + right)/2 ); else if (1 < right - leftIndex) { if (*(array + leftIndex + 1) > *(array + right)) swap (array + leftIndex + 1, array + right); } } int main (void) { clock_t start = clock(); int MAX = 10000000; int i; int * array = malloc(MAX * sizeof(int)); printf("FLOOR %d\n", (2+3)/2); if (array == NULL) { printf("Not enough memory"); return; } for (i=0;i<MAX;i++) array[i] = rand()%MAX; quicksort(array,0,MAX-1,(MAX-1)/2);

double duration = (double)clock() - start; printf("%lf", duration); return 0; }

You might also like