Hello,
I am trying to write the quick sort algorithm to take in an array of 20 random integers and sort them. However, it is producing weird output for the sorted list. My code is as follows, can anyone give me a little help here?
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
void q_sort(int list[], int left, int right);
void quicksort(int list[], int size)
{
q_sort(list, 0, size - 1);
}
void q_sort(int list[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = list;
while (left < right) {
while ((list >= pivot) && (left < right)) {
right--;
}
if (left != right) {
list = list;
left++;
}
while ((list <= pivot) && (left < right)) {
left++;
}
if (left != right) {
list = list;
right--;
}
}
list = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot) {
q_sort(list, left, pivot-1);
}
if (right > pivot) {
q_sort(list, pivot+1, right);
}
}
int main (void)
{
int random[20];
for (int i = 0; i < 20; i++) {
//add 20 random numbers to array
random[i] = rand();
cout << random[i] << "\n";
}
cout << "\n";
quicksort(random, 20);
cout << random << "\n";
return 0;
}