C Program for Quick Sort by VikramMulgund

VIEWS: 5 PAGES: 3

									/* www.mycfiles.com - C Program For Quick Sort */
#include<stdio.h>
#include<conio.h>
int arr[20];
int comp=0;//Variable For Comparison
int mov=0;//variable For Movements
int n;
void get();
void quick(int,int);
void show();
void main(){
clrscr();
printf("- This Program Explains Sorting Using Quick Sort - \n");
get();
quick(0,n-1);
show();
getch();


}


void get(){
      int i;
      while(1){
            printf("Enter the size of the elements :\n");
            scanf("%d",&n);
            if(n<=20)
                  break;
            else
                  printf("Sorry the maximum no of elements is 20\n\n");
            }
      printf("\n");
      printf("----------------------\n");
      printf("Enter the values \n");
      printf("----------------------\n\n");
      for(i=0;i<n;i++)
            scanf("%d",&arr[i]);
}
void quick(int l,int h) // l = Low,h = High
{
      int p,i,j;               // p = Pivot
      if(l>h)
            return;
      i=l+1;
      j=h;
      p=arr[l];
      while(i<=j){
            while((arr[i]<=p)&&(i<=h)){
                  i++;
                  comp++;
           }
           comp++;
           while((arr[j]>p)&&(j>=l)){
                 j--;
                 comp++;


           }
           comp++;
           if(i<j){
                 int t;
                 t=arr[i];
                 arr[i]=arr[j];
                 arr[j]=t;
                 mov++;


           }
           }
           if(l<j){
                 int t;
                 t=arr[l];
                 arr[l]=arr[j];
                 arr[j]=t;
                 mov++;
           }
           quick(l,j-1);
           quick(j+1,h);




}
void show(){
      int i;
      printf("\n");
      printf("-----------------------\n");
      printf("Sorted Array Elements\n");
      printf("-----------------------\n");
      for(i=0;i<n;i++){
            printf("%d\n",arr[i]);




     }
     printf("\nNumber Of Comparison : %d",comp);
     printf("\nNumber Of Movements : %d",mov);
}

								
To top