Segmentation fault (core dump)

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);

quick(array, low, pivot);
quick(array, pivot + 1, high);
}


int main()
{

int array[N];

for(int i = 0; i < N; i++)
{
array[i] = rand() % 100;
cout << array[i] << endl;
}
cout << "sorted data" << endl;

quick(array, 0, N -1);

for(int i = 0; i < N; i++)
cout << array[i] <<endl;
}

Last edited on
Your function quick. When will it ever end?
Note @Repeater's comment
1
2
3
4
5
6
7
8
9
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.
Topic archived. No new replies allowed.