Need Quicksort Help

I need to make a quicksort program that tells me how many swaps quickosrt goes through from a array that holds 10000 elements with a random number generator. I do have some code I have been trying to test but I can't get anything to show up on my compiler from my cout statements. Please help!

#include <iostream>

using namespace std;
int iaArray[50];
int iSize = 50;

void Partition(int* ipA, int iSize)
{

// Partitions of size 0 or 1 are already sorted
if (iSize <= 1)
{
return;
}

// Select a pivot from the array randomly
int iPivot = ipA[rand() % iSize + 1];

// Indices of the entries to be swapped
int iLower = 0;
int iUpper = iSize - 1;

// Partition array into sections above and below the pivot
while (iLower < iUpper)
{

while (ipA[iLower] < iPivot)
{
++iLower;
}

while (ipA[iUpper] > iPivot)
{
--iUpper;
}

// Swap the entries at the lower and upper indices
int iTemp = ipA[iLower];
ipA[iLower] = ipA[iUpper];
ipA[iUpper] = iTemp;
}

// Recursively call partition on each partititon.
Partition(ipA, iLower);
Partition(&(ipA[iLower + 1]), iSize - iLower - 1);
}

void Quicksort(int* ipA, int iSize)
{
// Seed the random number generator

for (int x=0; x<50; x++)
{
iaArray[x] = rand() % 50 + 1;
}
Partition(ipA, iSize);
}

int main()
{


Quicksort(iaArray, iSize);

// Output sorted array
for (int i = 0; i < iSize; i++)
{
cout << iaArray[i] << " ";
}
cout << endl;

cin.get();
return 0;
}
Try placing a

cout << "Partition(" << iSize << ")";

at the beginning of the Partition() function, to determine whether it is being called the number of times you expect. If not, perhaps there are some logic issues further down (which can be debugged by printing the variables and making sure they're changing as you'd expect).
Last edited on
Topic archived. No new replies allowed.