To sort an array of integers using Quick sort.
Program:-
#include<iostream.h> #include<conio.h> int partition(int[],int,int); void QuickSort(int[],int p,int r); void main() { int x,i,j,swap; clrscr(); int a[]={9,6,5,0,8,2,4,7}; cout<<"\nArray is: 9 6 5 0 8 2 4 7 \n"; QuickSort(a,0,7); for(i=0;i<8;i++) { cout<<a[i]<<" "; } getch(); } void QuickSort(int a[],int p,int r) { int q; if(p<r) { q=partition(a,p,r); QuickSort(a,p,q-1); QuickSort(a,q+1,r); }} int partition(int a[],int p,int r) { int swap,j; int x=a[r]; int i=p-1; for(j=p;j<=r-1;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; }
OUTPUT:-
(Visited 166 times, 1 visits today)
Written by: