Tuesday, 29 September 2015

Write a C Program To Perform Quick Sorting Technique

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void quicksort(int b[10],int first,int last);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before quick sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   quicksort(a,0,n-1);  
21:   printf("\n\nArray elements after quick sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void quicksort(int b[10],int first,int last)  
29:  {  
30:   int pivot,j,temp,i;  
31:   if(first<last)  
32:   {  
33:   pivot=first;  
34:   i=first;  
35:   j=last;  
36:   while(i<j)  
37:   {  
38:    while(b[i]<=b[pivot] && i<last)  
39:    i++;  
40:    while(b[j]>b[pivot])  
41:    j--;  
42:    if(i<j)  
43:    {  
44:    temp=b[i];  
45:    b[i]=b[j];  
46:    b[j]=temp;  
47:    }  
48:   }  
49:   temp=b[pivot];  
50:   b[pivot]=b[j];  
51:   b[j]=temp;  
52:   quicksort(b,first,j-1);  
53:   quicksort(b,j+1,last);  
54:   }  
55:  }  

No comments:

Post a Comment