0
help please
this function should sort the elements of the array,but it doesnt change anything in the array. void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); }
1 Odpowiedź
+ 4
Hello
This code is working correctly..
Perhaps there may be a problem when you call the function(incorrect left and right), or did not print the array afterwards to see it.
Remember, this code does not change the values in initialization, but change it for later use...
Try it like this :
#include<iostream>
using namespace std;
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
int main(void)
{
int arr[10]={1,2,4,62,2,25269,2552,7272,10,0};
quickSort(arr,0,10);
for(int i =0;i<10;i++)
cout<<arr[i]<<" ";
}