I am trying to partition an array using a quicksort algorithm shown in the code. I believe the problem is in the while loop. Can you see/explain what I'm doing wrong and what I should do to fix it? Thanks!
Edited because I posted an earlier version of the code.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int intArray[10];
int sizeArray= 10;
int main(){
srand(time(0));
for(int i = 0; i < sizeArray; i++){
//random numbers from 1 to 10:
intArray[i] = rand() % 100 + 1;
}
for(int i = 0; i < sizeArray; i++){
cout << intArray[i] << " ";
}
int *pivot = &intArray[0];
int *first = &intArray[1];
int *last = &intArray[9];
cout<<"pivot "<<*pivot <<endl;
while(first<last)
{
while(*first<*pivot)
{
first++;
}
while(*last>*pivot)
{
last--;
}
if(*first>*last)
{
int aSwap = 0;
aSwap = *first;
*first = *last;
*last = aSwap;
}
if((first-1) > last)
break;
}
int bSwap=0;
bSwap = *first;
*first= *pivot;
*pivot = bSwap;
cout<<"After partition"<<endl;
for(int i = 0; i < sizeArray; i++){
cout << intArray[i] << " ";
}
}