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.
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22
  • 2826901577 2018-02-22
  • 3647361576 2018-02-22
  • 5717551575 2018-02-22
  • 523811574 2018-02-22
  • 6439871573 2018-02-22
  • 8109431572 2018-02-22
  • 8757321571 2018-02-22
  • 5265111570 2018-02-22
  • 3351351569 2018-02-22
  • 5109361568 2018-02-22
  • 4455391567 2018-02-22
  • 9091121566 2018-02-22
  • 24791565 2018-02-22
  • 2486841564 2018-02-21
  • 9847231563 2018-02-21
  • 9264681562 2018-02-21