Vous êtes sur la page 1sur 19

PSG COLLEGE OF TECHNOLOGY

DESIGN OF ALGORITHM ANALYSIS LABORATORY

QUICK SORT ALGORITHM AND ANALYSIS

KMS.SRI GOBICAA (12Z354)

DISCLAIMER

I delare the following to be my own work unless otherwise referenced as defined by the college policy on plagiarism.

QUICK SORT:
Quicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoar. It can be done by recursion It uses DIVIDE AND CONQUER method Algorithm QuickSort(A, low, high) * A is an array of size N, containing the numbers to be sorted * low stores the subscript of the first element of the array or subarray and is initially 1 * high stores the subscript of the last element of the array or subarray and is initially n 1. if( low < high) //Stop the process when there is only one element 2. pivot = A[low] 3. i = low + 1 4. j = high 5. Repeat while i<j 6. Repeat while pivot > A[i] // Increment i till a number greater than pivot is found 7. 8. i=i+1 end repeat 9. Repeat while pivot < A[i] // Decrement j till a number less than pivot is found 10. 11. j=j-1 end repeat

12. 13. 14.

if (i<j) Swap(A[i], A[j]) end if

15. end Repeat 16. Swap(pivot, A[j]) // End of a pass and pivot element is in its right position 17. QuickSort(A, low, j-1) // Repeat for the subarray with numbers less than pivot 18. QuickSort(A, j+1, high) // Repeat for the subarray with numbers greater than pivot 19. end if 20. end Quicksort

Running time analysis Worst-Case(Data is sorted already) When the pivot is the smallest (or largest) element at partitioning on a block of size n, the result yields one empty sub-block, one element (pivot) in the correct place and one sub-block of size (n-1) takes (n) times. Recurrence Equation: T(1) = 1 T(n) = T(n-1) + cn Solution: (n^2)

Worser than merge sort!!!

Best case: The pivot is in the middle (median) (at each partition step), i.e. after each partitioning, on a block of size n, the result yields two sub-blocks of approximately equal size and the pivot element in the middle position takes n data comparisons. Recurrence Equation becomes T(1) = 1 T(n) = 2T(n/2) + cn Solution: (n logn) Comparable to merge sort!!!! Average case: It turns out the average case running time also is (n logn)

QUICK SORT WHERE PIVOT ELEMENT IS LAST #include<stdio.h> #include<conio.h> #include<time.h> void swap ( int* a, int* b ) { int t = *a; *a = *b; *b = t; }

int partition (intarr[], int l, int h) { int p = arr[h]; int i = (l - 1); int j;

for (j = l; j <= h- 1; j++) {

if (arr[j] <= p) { i++; swap (&arr[i], &arr[j]); } } swap (&arr[i + 1], &arr[h]); return (i + 1); } voidquickSort(int A[], int l, int h) { if (l < h) { int p = partition(A, l, h); quickSort(A, l, p - 1); quickSort(A, p + 1, h); } } voidprintArr( intarr[], int x) { int i; for ( i = 0; i < x; ++i )

printf( "%d ", arr[i] ); }

int main() { intarr[] = {90, 10,82,111,28,50,46,83,170,1200,1400,546,320}; clock_t start, end;

start = clock();//time count starts

int n = sizeof( arr ) / sizeof( *arr ); printf("Quick sort where pivot is last element :\n\n");

quickSort(arr, 0, n - 1 );

printf("After sorting \n\n"); printArr(arr, n ); printf("\n"); printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);

printf("\n\n"); return 0;}

OUTPUT

QUICK SORT WHERE PIVOT ELEMENT IS FIRST #include<stdio.h> #include<time.h> void quicksort(int [10],int,int);

int main(){ intarr[]={90, 10,82,111,28,50,46,83,170,1200,1400,546,320},i,n; doubletotal_time; clock_t start, end;

start = clock();//time count starts printf("\t\t\tQUICK SORT\n "); printf("Pivot is first element:\n\n");

n = sizeof(arr ) / sizeof( *arr ); quicksort(arr, 0, n - 1 );

printf("Sorted elements: "); for(i=0;i<n;i++) printf(" %d",arr[i]); printf("\n\n"); end = clock();//time count stops

printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); printf("\n\n");

return 0; }

void quicksort(int x[10],intfirst,int last){ intpivot,j,temp,i;

if(first<last){ pivot=first; i=first; j=last;

while(i<j){ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--;

if(i<j){ temp=x[i]; x[i]=x[j]; x[j]=temp; }} temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last);} } OUTPUT

QUICK SORT WHERE PIVOT ELEMENT IS MIDDLE

#include<stdio.h> #include<conio.h> #include<time.h> void swap ( int* a, int* b ) { int t = *a; *a = *b; *b = t; } void quicksort(int a[],intl,int h) { intp,left,right;

left=l; right=h; p=a[(left+right)/2];

while(right>left) { while(a[right]>p) { right--; } while(a[left]<p) { left++; } if(left<=right) { swap(&a[left],&a[right]); left++; right--; } } if(l<right)

quicksort(a,l,right); if(left<h) quicksort(a,left,h); }

voidprintArr( intarr[], int x) { int i; for ( i = 0; i < x; ++i ) printf( "%d ", arr[i] ); }

int main() { intarr[] = {90, 10,82,111,28,50,46,83,170,1200,1400,546,320}; doubletotal_time; clock_t start, end;

start = clock();//time count starts

int n = sizeof( arr ) / sizeof( *arr ); quicksort(arr, 0, n - 1 );

printf("Quick sort where pivot is middle element :\n\n");

printf("After sorting \n\n"); printArr(arr, n ); printf("\n\n"); end = clock();//time count stops printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); printf("\n\n");

return 0; }

OUTPUT

GRAPH: TIME OF EXECUTION VS ELEMENT POSITION

TIME
0.0045 0.004 0.0035 0.003 0.0025 0.002 0.0015 0.001 0.0005 0 FIRST MIDDLE LAST TIME

CONCLUSION : The above program has been carried out successfully and execution time shown graphically for different position of pivot element and output verified.

Vous aimerez peut-être aussi