I'm testing some case of quick sort in visual studio but I got and error and I'm not sure what's wrong with my code.
Here's my code.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void Swap(int *arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void BubbleSort(int *arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
Swap(arr, j, j + 1);
}
}
}
}
void QuickSort(int *arr, int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int low = left;
int high = right + 1;
while (low <= high) {
while (arr[++low] <= pivot && low <= right);
while (arr[--high] >= pivot && high > left);
if (low > high) {
Swap(arr, left, high);
}
else {
Swap(arr, low, high);
}
}
QuickSort(arr, left, high - 1);
QuickSort(arr, high + 1, right);
}
int main() {
srand((unsigned)time(NULL));
int *arr = new int[10000];
for (int i = 0; i < 10000; i++) {
arr[i] = rand() % 10000;
}
BubbleSort(arr, 10000);
QuickSort(arr, 0, 9999);
delete[] arr;
return 0;
}
It doesn't matter if I allocate little size of memory but this code's case got an error with my QuickSort() function.