Hi,
I'm trying to learn quick sort. Right now, the code compiles but it shows me a segmentation error (core dump). I have tried to look it up online but I'm new to linux and I don't know how to go about fixing the issue. So far my code compiles the unsorted data in the main and shows me a 'segmentation error (core dump)' right after without telling me where in my code the problem is. I'm posting my code below:
#include <iostream>
#include <cstdlib>
#define N 15
using namespace std;
int partition(int array[], int low, int high)
{
int pivot = array[low];
int i = low - 1;
int j = high + 1;
while (1)
{
do {
i++;
} while (array[i] < pivot);
do {
j--;
} while (array[j] > pivot);
if (i >= j)
return j;
swap (array[i], array[j]);
}
}
void quick(int array[], int low, int high)
{
int pivot = partition(array, low, high);
void quick( int array[], int low, int high )
{
int pivot = partition( array, low, high );
if ( low < high ) // <=====
{ // <=====
quick( array, low, pivot );
quick( array, pivot + 1, high );
} // <=====
}
Please use code tags.
Probably need a call to srand() in main as well - minor issue.
Those kind of errors could have been spotted earlier if detected while coding. I recommend you to do that. If you are having troubles doing that you can use any software to help you, such as Checkmarx I once used.
Anyway, good luck.