#include<iostream>
using namespace std;
void swap(int array[], int index1, int index2){
int t = array[index1];
array[index1] = array[index2];
array[index2] = t;
return;
}
int partition(int array[], int beginPos, int endPos)
{
int comparedVal = array[beginPos];
int cursorFromBegin = beginPos + 1;
int cursorFromEnd = endPos;
while(true){
while (array[cursorFromBegin] < comparedVal){cursorFromBegin++;}
while (array[cursorFromEnd] > comparedVal){cursorFromEnd--;}
if (cursorFromBegin>=cursorFromEnd){break;}
swap(array, cursorFromBegin, cursorFromEnd);
}
swap(array,cursorFromEnd,beginPos);
return cursorFromEnd;
}
void quickSort(int array[],int begin,int end){
if (begin >= end){cout<<begin<<end<<endl;return;}
int k = partition(array,begin,end);
quickSort(array, begin,k-1);
quickSort(array, k+1, end);
}
int main(){
int array[6]={3,5,9,8,7,5};
quickSort(array, 0, 5);
for(int i =0;i<6;i++){cout<<array[i]<<endl;}
}