C++ programs – Sorting numbers in ascending order using Quick sort method

by Nideesh C on April 14, 2011 · 0 comments

in C++




#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout <<"enter 10 elements";
for(i=0;i<10;i++)
cin >> a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"sorted elements";
for(i=0;i<10;i++)
cout << a[i] << " ";
getch();
}
 
void quick(int a[],int l,int u)
{
   int p,temp;
   if(l<u)
   {
   p=a[l];
   i=l;
   j=u;
    while(i<j)
   {
      while(a[i] <= p && i<j )
	 i++;
      while(a[j]>p && i<=j )
	   j--;
      if(i<=j)
      {
      temp=a[i];
      a[i]=a[j];
      a[j]=temp;}
  }
  temp=a[j];
  a[j]=a[l];
  a[l]=temp;
  cout <<"\n";
  for(i=0;i<10;i++)
  cout <<a[i]<<" ";
  quick(a,l,j-1);
  quick(a,j+1,u);
 }
}

OUTPUT

enter 10 elements5 2 3 16 25 1 20 7 8 61 14
 1 2 3 5 25 16 20 7 8 61
 1 2 3 5 25 16 20 7 8 61
 1 2 3 5 25 16 20 7 8 61
 1 2 3 5 25 16 20 7 8 61

 1 2 3 5 25 16 20 7 8 61
 1 2 3 5 8 16 20 7 25 61
 1 2 3 5 7 8 20 16 25 61
 1 2 3 5 7 8 16 20 25 61
 1 2 3 5 7 8 16 20 25 61
sorted elements1 2 3 5 7 8 16 20 25 61



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ programs for sorting a given numbers in ascending order using Merge sort
  2. C program to sort N numbers in ascending order array with suitable headings
  3. C++ program to implement Quick sort Algorithm using class
  4. C program to accept a amtric of order MxN and sort all rows of the matrix in ascending order and all columns in descendng order
  5. C program to sort given N elements using SELECTION sort method using functions a) To find maximum of elements b) To swap two elements

Leave a Comment

Previous post:

Next post: