Tuesday, 22 January 2013

Quick Sort


#include<stdio.h>
#include<conio.h>
int partition(int a[], int low, int high)
                {
                    int i,j,key,temp;
                    key=a[low];
                    i=low+1;
                    j=high;
                    while(1)
                    {
                                            while(i<high&&key>=a[i])
                            i++;
                            while(key<a[j])
                            j--;
                            if(i<j)
                            {
                                   temp=a[i];
                                   a[i]=a[j];
                                   a[j]=temp;
                                   }
                                   else
                                   {
                                   temp=a[low];
                                   a[low]=a[j];
                                   a[j]=temp;
                                   return j;
                                   }
                          }
               }
               
int quicksort(int a[],int low, int high)
{
    int j;
    if(low<high)
    {
                j=partition(a,low,high);
                quicksort(a,low,j-1);
                quicksort(a,j+1,high);
    }
 }
                                   int main()
                                   {
                                       int a[10],i,n;
                                       printf("enter array limitn");
                                       scanf("%d",&n);
                                       printf("enter array elementsn");
                                       for(i=0;i<n;i++)
                                       scanf("%d",&a[i]);
                                       quicksort(a,0,n-1);
                                       printf("sorted elements n");
                                       for(i=0;i<n;i++)
                                       printf("%dn",a[i]);
                                       getch();
                                   }





No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016