Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Quick Sort library function in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 359
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: