Quick sort can be very useful to you if you want to sort your array efficiently.
The Time complexities for quick sort
Best Case performance : O(n log n)
Average Case Performance : O(n log n)
Worst Case Performance : O(n2)
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
Following is the declaration for qsort() function.
void qsort(void base, size_t nitems, size_t size, int (compar)(const void , const void))
Demo Program:
#include <stdio.h>
#include <stdlib.h>
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int n;
printf("Before sorting the list is: \n");
for( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\nAfter sorting the list is: \n");
for( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
}
return(0);
}
Output
Before sorting the list is:
88 56 100 2 25
After sorting the list is:
2 25 56 88 100
0 Comment(s)