Having a problem with my quicksort algorithm. The code compiles without any errors, but when I try to run the program the only output I get is "The random numbers are: " and then acts like it wants input from the user, and then I have to kill the program. Now when I remove the call for my quicksort function in my main program, the program prints out the numbers, but doesn't work with the call for quicksort function. I'm not sure if the parameters I'm using are the issue or if it's the function itself.
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
void quicksort(int arr[], int left, int right) {
int l = left;
int r = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (l <= r) {
while (arr[l] < pivot)
l++;
while (arr[l] > pivot)
r--;
if (l <= r) {
tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++;
r--;
}
}
if (left < r)
quicksort(arr, left, r);
if (l < right)
quicksort(arr, r, right);
}
int main() {
int n = 20;
int testlist[n];
for (int i = 0; i<n; i++) {
testlist[i] = rand()%100;
}
cout << "The random numbers are: " << endl;
for (int i = 0; i < n; i++) cout << testlist[i] << " ";
quicksort(testlist, 0, n - 1);
cout << " " << endl;
cout << "The sorted numbers are: " << endl;
for (int i = 0; i < n; i++) {
cout << testlist[i] << " ";
}
return 0;
}
l(the letter L in lowercase) looks a lot like1which is confusing when reading code.