2

Hello when I try to compile the following code, it says segmentation fault:11 but I do not understand why? I also do not understand what does the error mean. Can someone please explain to me? Thanks for your time.

# include <iostream>
using namespace std;

void merge(int *left,int *right,int *list){
    int lenL = sizeof(left)/sizeof(left[0]);
    int lenR = sizeof(right)/sizeof(right[0]);
    int len = sizeof(list)/sizeof(list[0]);
    int i = 0, j =0,  k =0;

    while(i<= lenL && j<= lenR){

     if(left[i] <= right[j]){
        list[k] = left[i];
        i++;
       }
     else{
        list[k]=right[j];
        j++;
         }
    k++;
    }

while(i<= lenL){
    list[k] = left[i];
    i++;k++;
}

while(j<= lenR){
    list[k] = right[j];
    j++;k++;
  }

}

void mergesort(int *list){
    int length = sizeof(list)/sizeof(list[0]);
    if (length < 2){
    return;
   }

    int mid = length/2;
    int left[mid];
    int right[length-mid];

for (int i=0; i< mid-1; i++){
    left[i]=list[i];
    }

for (int i=mid; i< length-1; i++){
    right[i]=list[i];
    }
mergesort(left);
mergesort(right);
merge(left,right,list);

}

int main(){
    int array [] = {2,5,4,3};
    mergesort(array);

return 0;
}
Kirsteen Ng
  • 111
  • 1
  • 8

0 Answers0