0
I tried to recreate the Mergesort algorithm in C++, but when compiling, the error "segmentation fault" appears. Here is the code below. What could I be doing wrong?
#include <iostream>
using namespace std;
void merge (int *arr, int lo, int m, int hi){
int n1, n2;
n1 = m - lo + 1; //maximo do subarray1
n2 = hi - m; //maximo do subarray2
int *L, *R, i, j, k;
for(i=lo; i<n1; i++){
L[i]=arr[lo+i]; //cria array temporario para armazenar uma subarray
} //obs: vai de lo ateh n1-1
for (j = hi; j< n2; j++){
R[j]=arr[m+1+j]; //cria array temporario para armazenar outra subarray
}// obs vai de m+1 ateh n2-1
i=0;
j=0;
k=lo; // comparar os subarrays e preencher o arr com os menores elementos de cada sub por vez
while (i<n1 && j<n2){
if (L[i]<=R[j]){
arr[k]=L[i]; //se o de L for menor, arr recebe de L
i++;
}
else {
arr[k]=R[j]; //se o de R for menor, arr recebe de R
j++;
}
k++; //vai preenchendo arr[k]
}
while (i<n1){ // termina de preencher arr com o q falta de L
arr[k] = L[i];
i++;
k++;
}
while (j<n2){ // termina de preencher arr com o q falta de R
arr[k] = R[j];
j++;
k++;
}
}
void mergesort(int *arr, int lo, int hi){ //dividir e conquistar
if (lo < hi){
int m;
m = (hi + lo)/2;
mergesort (arr, lo, m);
mergesort (arr, m+1, hi);
merge (arr, lo, m, hi);
}
}
void printarr(int *arr){ //imprime array ordenado
for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++){
cout << arr[i] << " ";
}
}
int main() {
int *vetor, N;
cin >> N;
for (int i = 0; i<N; i++){
cin >> vetor[i];
}
mergesort(vetor, 0, N);
printarr(vetor);
return 0;
}
The first obvious problem is that you are not initiating memory to count the vector, there may be others.
– Maniero
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero