DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

QuickSort.cpp

02.01.2008
| 5824 views |
  • submit to reddit
        #include <iostream>

using namespace std;


void show (int size, float *a)
{
  int i;
  for (i = 0; i < size; i++)
    cout << a[i] << "  ";
  cout << endl;
}
void sort (int size, float *a, int pivotindex)
{
  int smidx, idx;
  float temp;
  float pivot = a[pivotindex];
  show (9, a);
  a[pivotindex] = a[0];
  a[0] = pivot;
  cout << "swap(" << 0 << ", " << pivotindex << ")";
  cout << "   (pivot = " << pivot <<  ")" << endl;
  show (9, a);
  smidx = 0;
  for (idx = 1; idx < size; idx++)
  {
      if ( a[idx] < pivot )
      {
		smidx++;
        temp = a[smidx];
        a[smidx] = a[idx];
        a[idx] = temp;
  	    cout << "swap(" << smidx << ", " << idx << ")" << endl;
		show (9,a);
      }
    }
   temp = a[smidx];
   a[smidx] = a[0];
   a[0] = temp;
   cout << "swap(" << 0 << ", " << smidx << ")" << endl;
   show (9,a);
}


void main(void)
{
  static float a[] = {45, 82, 25, 94, 50, 60, 78, 32, 92};
  sort (9, a, 4);
}