0

I tried with iterative way and chose pivot element as the last element of the array. I executed it on Hacker Earth which raised the sigsegv error where the array size ranged from 1 to 10^6 and array element size from 1 to 10^9. When I tried on merge sort, it did so efficiently, it didn't even raise fault. How did that happen? Following is the code that was implemented.

// An iterative implementation of quick sort        /* working*/
#include <iostream>
using namespace std;
void swap ( long int &a, long int &b )
{
    long int t = a;
    a = b;
    b = t;
}

int partition (int arr[],long int l,long int h)
{
    long int x = arr[h];
    long int i = l;
    long int j=h-1;
    while(i<j){
        if(arr[i]<x){
            i++;
        }
        if(arr[j]>x){
            j--;
            continue;
        }
        if(arr[i]>x&&arr[j]<x){
            swap(arr[i],arr[j]);
            i++;
            j--;
        }
    }
    swap(arr[i],arr[h]);
    return i;
}
void quickSortIterative (int arr[], int l, long int h)
{
    int stack[2];
    int top = -1;
    stack[ ++top ] = l; 
    stack[ ++top ] = h; 
    while ( top >= 0 )
    {
        h = stack[ top-- ];
        l = stack[ top-- ];
        long int p = partition( arr, l, h );
        if ( p-1 > l )
        {
            stack[ ++top ] = l;
            stack[ ++top ] = p - 1;
        }
        if ( p+1 < h )
        {
            stack[ ++top ] = p+1;
            stack[ ++top ] = h;
        }
    }
}

int main()
{
    long int N;
    cin>>N;
    long int i,j;
    int arr[N];
    for(i=0;i<N;i++){
        cin>>arr[i];
    }
    quickSortIterative( arr, 0, N - 1 );
    for ( i = 0; i < N; ++i ){
        cout<<arr[i]<<" ";
    }
    return 0;

}

B Uttej
  • 5
  • 5
  • You need to paste some code, especially the part which you think is wrong. – Arunmu Nov 08 '16 at 05:10
  • @Arunmu I have done it. – B Uttej Nov 08 '16 at 05:24
  • `int arr[N];` is not standard c++. Either use a vector or global array with a known max size. You do not have to implement `swap` and `partition`, there is already an efficient implementation available in the standard library intuitively named as `std::swap` and `std::partition` – Arunmu Nov 08 '16 at 05:36
  • Possible duplicate of [Why do I get a segfault in C from declaring a large array on the stack?](http://stackoverflow.com/questions/3144135/why-do-i-get-a-segfault-in-c-from-declaring-a-large-array-on-the-stack) – n. m. could be an AI Nov 08 '16 at 06:14

2 Answers2

0

Allocating large arrays in local variables is a surefire way to trigger a stack overflow — the stack in a typical execution environment simply isn't set up to handle such things.

You need to allocate your arrays on the heap; e.g. by using std::vector. Your code would be more portable too, since variable length arrays aren't part of the C++ standard.

0

stack[2] only has room for 2 integers, but the code "pushes" more than two values onto the stack. Consider the first loop where p-1 > l && p+1 < h, in which case top is incremented 4 times. You could make stack a vector or a stack using the equivalent of push and pop to store and load values.

int arr[N] will also fail for large N. Since this is C++, consider using:

int * arr = new int[N];

you'll need to delete[] arr at the end of main.

rcgldr
  • 27,407
  • 3
  • 36
  • 61