Quick Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int m=1, n, a[20];  
4:  void main( )  
5:  {  
6:        void quick(int low,int high, int n);  
7:        int i;  
8:        clrscr();  
9:        printf("Enter array size :");  
10:        scanf("%d",&n);  
11:        printf("\nEnter the elements : \n");  
12:        for(i=0;i<n;i++)  
13:         scanf("%d",&a[i]);  
14:        clrscr();  
15:        quick(0,n-1, n);  
16:        printf("\n\n\t\tSORTED ARRAY\n\t");  
17:             for(i=0;i<n;i++)  
18:           printf("%d\t",a[i]);  
19:        getch();  
20:  }  
21:  void quick(int low,int high, int n)  
22:  {  
23:          int p,j,temp,i, k;  
24:          if(low<high)  
25:          {  
26:             p=low;  
27:             i=low;  
28:             j=high;  
29:             while(i<j)  
30:             {  
31:                    while((a[i]<=a[p])&&(i<high))  
32:                          i++;  
33:                    while(a[j]>a[p])  
34:                          j--;  
35:                    if(i<j)  
36:                    {  
37:                       temp=a[i];  
38:                       a[i]=a[j];  
39:                       a[j]=temp;  
40:                    }  
41:             }  
42:             temp=a[p];  
43:             a[p]=a[j];  
44:             a[j]=temp;  
45:             if(m<n-1)  
46:             {  
47:                    printf("\n\t\tAfter scan %d\n\t", m);  
48:                    for(k=0;k<n;k++)  
49:                       printf("%d \t", a[k]);  
50:                    printf("\n");  
51:             }  
52:             m++;  
53:             quick(low,j-1,n);  
54:             quick(j+1,high,n);  
55:         }  
56:  }  

No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive