-1

I am learning the Merge sorting technique and wrote this code by following a tutorial. However, I am getting an error here. I would like to understand the mistakes, I am doing here. On using online compiler I encountered Segmentation error, while locally, error like operator not matching is found.

#include <bits/stdc++.h>
using namespace std;

void merge(vector<int> &arr, int low, int mid, int high){
    vector<int> temp;
    int left=low;`enter code here`
    int right=mid+1;
    while (left<=mid && right<=high)
    {
        if(arr[left]<=arr[right]){
            temp.push_back(arr[left]);
            left++;
        }
        else{
            temp.push_back(arr[right]);
            right++;
        }
    }
    while(left<=mid){
        temp.push_back(arr[left]);
            left++;
    }
    while(right<=high){
        temp.push_back(arr[right]);
        right++;
    }
    for(int i=low;i<=high;i++){
        arr[i]=temp[i-low];
    }
}

void mS(vector<int> &arr, int low, int high){
    if(low>=high) return;
    int mid=(low+high)/2;
    mS(arr, low, mid);
    mS(arr, mid+1, high);
    merge(arr, low, mid, high);
}

void mergeSort(vector<int> &arr, int n){
    mS(arr, 0, n-1);
}
int main()
{
    int n;
    cin >> n;
    vector<int> arr;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    mergeSort(arr, n);
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
}
Evg
  • 25,259
  • 5
  • 41
  • 83
  • 2
    OT: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) No matter what you do, bad habits like those should be avoided. – Some programmer dude May 26 '23 at 07:47
  • As for the "errors" you get, what kind of errors are they? If you get build errors or warnings, then please copy-paste the complete and full build-log into your question, and add comments in the code where you get those errors. If you get crashes, use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to locate when and where in your code you get them. If you get unexpected results, then tell us the input you give, together with the actual and expected output. And debug your code. – Some programmer dude May 26 '23 at 07:49
  • 1
    Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – and even more important: about [including `bits/stdc++.h`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)! – Aconcagua May 26 '23 at 08:27

1 Answers1

2

vector<int> arr; is an empty vector. cin >> arr[i]; writes to a non existing array.

Change vector<int> arr; to vector<int> arr(n); to initialize the vector with n elements.

After that it looks like the program is running: https://godbolt.org/z/szvzeoxbW

mch
  • 9,424
  • 2
  • 28
  • 42