Académique Documents
Professionnel Documents
Culture Documents
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
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)
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;
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 )
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);
OUTPUT
QUICK SORT WHERE PIVOT ELEMENT IS FIRST #include<stdio.h> #include<time.h> void quicksort(int [10],int,int);
start = clock();//time count starts printf("\t\t\tQUICK SORT\n "); printf("Pivot is first element:\n\n");
printf("Sorted elements: "); for(i=0;i<n;i++) printf(" %d",arr[i]); printf("\n\n"); end = clock();//time count stops
return 0; }
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
#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;
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)
voidprintArr( intarr[], int x) { int i; for ( i = 0; i < x; ++i ) printf( "%d ", arr[i] ); }
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
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.