Quick Sort Program

#include<iostream.h> #include<conio.h> int partition(int a[],int p,int r) { int x,i,temp; x=a[r]; i=p-1; for(int j=p;j<r;j++) { if(a[j]<=x) { i=i+1; temp=a[j]; a[j]=a[i]; a[i]=temp; } } temp=a[i+1]; a[i+1]=a[j]; a[j]=temp; return i+1; } 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); } } void main() { clrscr(); int *a,n; cout<<"\nEnter the no. of element you want:"; cin>>n; a=new int[n]; //dynamically initialize array cout<<"\nEnter the elements:\n"; for(int i=0;i<n;i++) cin>>a[i]; quicksort(a,0,n-1); cout<<"\nSorted Array is:\n"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); }

Comments

Popular posts from this blog