Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
891 B
2
Indexable
Never
if (arr.size()>1) {
        int mid = arr.size()/2;
        vector<vector<int>> lefthalf(arr.begin(),arr.begin()+mid);
        vector<vector<int>> righthalf(arr.begin()+mid,arr.end());

        lefthalf = mergeSort(lefthalf);
        righthalf = mergeSort(righthalf);

        int i = 0;
        int j = 0;
        int k = 0;
        while (i < lefthalf.size() && j < righthalf.size()) {
            if (lefthalf[i][0] > righthalf[j][0]) {
                arr[k] = lefthalf[i];
                i++;
            } else {
                arr[k] = righthalf[j];
                j++;
            }
            k++;
        }

        while (i<lefthalf.size()) {
            arr[k] = lefthalf[i];
            i++;
            k++;
        }

        while (j<righthalf.size()) {
            arr[k]=righthalf[j];
            j++;
            k++;
        }

    }
Leave a Comment