- #include<stdio.h>
- int partition(int a[],int p,int r)
- {
- int i,j,swap,x;
- x= a[r];
- i= p-1;
- for(j=p;j<r;j++)
- {
- if(a[j]<=x)
- {
- i=i+1;
- swap=a[i];
- a[i]=a[j];
- a[j]=swap;
- }
- }
- swap=a[i+1];
- a[i+1]=a[r];
- a[r]=swap;
- return (i+1);
- }
- void quick_sort(int a[], int p, int r)
- {
- int q;
- if(p<r)
- {
- q = partition(a,p,r);
- quick_sort(a,p,q-1);
- quick_sort(a,q+1,r);
- }
- }
- int main()
- {
- int a[10],i,j,n;
- printf("Enter the number of elements:\n");
- scanf("%d",&n);
- printf("Enter the elements:\n");
- for(i=0;i<n;i++)
- {
- scanf("%d",&a[i]);
- }
- printf("The given array is:\n");
- for(i=0;i<n;i++)
- {
- printf("%d\t",a[i]);
- }
- printf("\n");
- quick_sort(a,0,n-1);
- printf("The Sorted array is:\n");
- for(i=0;i<n;i++)
- {
- printf("%d\t",a[i]);
- }
- return 0;
- }
[text] Quick-Sort
Viewer
Editor
You can edit this paste and save as new: