hi all,

when i run this code (quick sort), i ll get ascending order as output... now i want descending order instead... anybody know how to altert this code..uhuhuh

void QuickSorting::sort(int a[], int lo, int up)
{
  {
   int i, j, pivot;
     while ( up>lo )
     {
	  i = lo;
	  j = up;
	  pivot = a[lo];

	  while ( i<j )
	  {
		  for ( ; a[j] > pivot; j-- );
		  for ( a[i]=a[j]; i<j && a[i]<=pivot; i++ );
				  a[j] = a[i];
	  }
	  a[i] = pivot;

	  if ( i-lo < up-i )
		  { sort(a,lo,i-1);  lo = i+1; }
	  else
		  { sort(a,i+1,up);  up = i-1; }
     }
  }
}

output: 12345 -- would like to change to : 54321

Member Avatar for iamthwee

At a guess try changing if ( i-lo < up-i ) to if ( i-lo > up-i )?

hehe the same output... :)

Member Avatar for iamthwee

What's your main function like?

Post it.

hi amtheweeeeeeee's :)

i attached my coding here...

#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdio.h>
#include <string.h>
#define Size 50

class QuickSorting
{
	private:
   	int a[Size];
      int up, lo, pivot, temp;

   public:
   	void read(int []);
      void sort(int[], int lo, int up);
      void display(int []);

};

//read from input file
void QuickSorting::read(int A[])
{
int x=0;
int y=50;
  ifstream inputFile;
  inputFile.open("d:\\dataM.txt");
{  if (!inputFile)
  {
		cout << "While opening a file an error is encountered" << endl;
      return;
  }
  while(x<y && inputFile>>A[x])
  x++;
}
  cout<<"\nUnsorted data....\n";
  for(int i=0;i<Size;i++)
  cout<<setw(3)<<A[i]<<" ";
  inputFile.close();
 }
//read from file finish


void QuickSorting::sort(int a[], int lo, int up)
{
  {
   int i, j, pivot;
     while ( up>lo )
     {
	  i = lo;
	  j = up;
	  pivot = a[lo];

	  while ( i<j )
	  {
		  for ( ; a[j] > pivot; j-- );
		  for ( a[i]=a[j]; i<j && a[i]<=pivot; i++ );
				  a[j] = a[i];
	  }
	  a[i] = pivot;

	 if ( i-lo > up-i )
		  { sort(a,lo,i-1);  lo = i+1; }
	  else
		  { sort(a,i+1,up);  up = i-1; }
     }
  }
}

void QuickSorting:: display (int a[])
{
 cout<<"\n\nSorted data -QuickSorting-......\n";
   for(int i=0;i<Size;i++)
   cout<<setw(3)<<a[i]<<" ";
 }

int main()
{
const int s=50;
int a[s];
QuickSorting QS;
QS.read(a);
QS.sort(a,0,s+1);
QS.display(a);
getch();
return 0;
}

thanks.. i've tried to change it. but its not what i want.. thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.