Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

QuickSort.cpp

DZone's Guide to

QuickSort.cpp

·
Free Resource
#include 
 
  

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);
}


 
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}