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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 366
    Comment on it

    Quick Sort Algorithm:- Quick sort is the most famous sorting algorithm. It is the external sorting technique that is based on divide and conquer strategy. The Complexity of this algorithm is following:

    Best Case:- O(n log n)
    Average Case:- O(n log n)
    Worst Case:- O(n^2)


    #include<stdio.h>
    #include<conio.h>
    
    int split(int *,int,int);
    void quick_sort(int *,int,int);
    
    void main()
    {
    int arr[10],i,j,lw,up,mid;
    clrscr();
    printf("\nEnter 5 Unsorted Elements:=");
    for(i=0;i<6;i++)
    scanf("%d",&arr[i]);
    
    lw=0;
    up=6-1;
    quick&#95;sort(arr,lw,up);
    printf("\nSorted Array is:=\n");
    for(i=0;i<6;i++)
    printf("\n%d",arr[i]);
    getch();
    }
    
    void quick_sort(int *a,int lw,int up)
    {
    int i;
        if(up>lw)
        {
            i=split(a,lw,up);
            quick_sort(a,lw,i-1);
            quick_sort(a,i+1,up);
        }
    }
    
    int split(int *a,int lw,int up)
    {
    int i=0,j=0,pivot,temp;
    i=lw+1;
    j=up;
    pivot=a[lw];
    
    
    while(j>=i)
    {
        while(a[i]<pivot)
        i++;
    
        while(pivot<a[j])
        j--;
    
        if(j>i)
        {
           temp=a[i];
           a[i]=a[j];
           a[j]=temp;
        }
    
    }
    temp=a[lw];
    a[lw]=a[j];
    a[j]=temp;
    
    return j;
    }
    

 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: