Quicksorting with Recursion

This program uses recursion in order to quickly sort an array of integers. The program sorts the integers but I'm unsure if it was done correctly due to it not being in order. Is there any small changes this code needs?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream>
using namespace std;

//Function prototypes
void quickSort(int arr[], int start, int end);
int partition(int arr[], int start, int end);
void swap(int &value1, int &value2);

int main()
{
int arr[9] = { 4, 8, 2, 7, 1, 5, 9, 6, 3 };
int i;

//Displays initial array
for (i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;

quickSort(arr, 0, 8);

//Displays sorted array
for (i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;


return 0;
}

//Function definitions
void quickSort(int arr[], int start, int end)
{
//index
int pivot;

if (start < end)
{
pivot = partition(arr, start, end);
//sort to the left sublist
quickSort(arr, start, pivot - 1);
//sort to the right sublist
quickSort(arr, pivot + 1, end);
}
}

int partition(int arr[], int start, int end)
{
int pivot;
int pivotIndex;

pivot = arr[end];
pivotIndex = (start - 1);

for (int index = start; index <= end - 1; index++)
{
if (arr[index] <= pivot)
{
pivotIndex++;
swap(arr[pivotIndex], arr[index]);
}
}
swap(arr[pivot], arr[end]);

return (pivotIndex + 1);
}
void swap(int &value1, int &value2)
{
int temp;
temp = value1;
value1 = value2;
value2 = temp;
}
Last edited on
Topic archived. No new replies allowed.