I need to make a quicksort program that tells me how many swaps quicksortt goes through from an 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. What's wrong with this code?
#include <iostream>
usingnamespace 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;
}
while (ipA[iLower] < iPivot)
{
++iLower;
}
while (ipA[iUpper] > iPivot)
{
--iUpper;
¿What will happen when the 'Lower' index reaches the pivot element? As a suggestion, move the pivot to the "end" of the array and change the condition to while (ipA[iUpper] >= iPivot)
Also int iPivot = ipA[rand() % iSize + 1]; That's out of bounds.