Thursday 6 March 2014

program of Quick Sort

#include<stdio.h>
#include<conio.h>

void qsort(int, int, int) ;     //Prototype

int main( )
{
    int arr[30];
    int i, size ;
    printf("Enter total number of Elements:  ") ;
    scanf("%d", &size);
    printf("Enter the Elements: \n")
    for(i=0; i<size; i++)
       scanf("%d", &arr[i]) ;
    qsort(arr, 0, size-1) ;    //calling
    printf("Quick Sorted elements are as:  \n") ;
    for(i=0; i<size; i++)
       printf("%d\t",arr[i]);

    return 0;
}

void qsort(int arr[20], int frst, int last)     //definition
 {
   int i, j, pivot, tmp;
     if(frst<last)
        {
            pivot=frst ;
            i=frst ;
            j=last;
           while(i<j)
             {
                while(arr[i]<=arr[pivot] && i<last)
                      i++  ;

                while(arr[j]>arr[pivot])
                      j-- ;
                if(i<j)
                   {
                         tmp=arr[i];
                         arr[i]=arr[j] ;
                         arr[j]=tmp;
                    }
             }                     //end of while loop
        tmp=arr[pivot] ;
        arr[pivot]= arr[j];
        arr[j]=tmp ;
        qsort(arr,frst,j=1) ;
        qsort(arr, j+1, last) ;
     }                    //end of if statement


}

No comments:

Post a Comment