// MergeSort.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include #include #include using namespace std; vector merge(vector arr, int left, int...

1 answer below »
Help Debug the code. C++ vector merge sort in descending order. Right now it is increasing order. The program receive a list of integer
numbers separated by a comma until the user enters *. Example of Input:3,6,1,0,5,6,-3,-17*. Should return output :6,6,5,3,1,0,-3,-17.


// MergeSort.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include #include #include using namespace std; vector merge(vectorarr, int left, int middle, int right) { int n1 = middle - left + 1; int n2 = right - middle; //int L[n1], R[n2]; vector L; vector R; for (int i = 0; i < n1;="" i++)="" {="" l[i]="arr[left" +="" i];="" l.push_back(arr[left="" +="" i]);="" }="" for="" (int="" i="0;" i="">< n2;="" i++)="" {="" r[i]="arr[middle" +1+="" i];="" r.push_back(arr[middle+1+i]);="" }=""> result; int i = 0; int j = 0; int k = left; while (i < n1="" &&="" j="">< n2)="" {="" if="" (l[i]=""><= r[j])="" {="" arr[k]="L[i];" i++;="" }="" else="" {="" arr[k]="R[j];" j++;="" }="" k++;="" }="" while="" (i="">< n1)="" {="" arr[k]="L[i];" i++;="" k++;="" }="" while="" (j="">< n2)="" {="" arr[k]="R[j];" j++;="" k++;="" }="" return="" arr;="" }=""> mergeSort(vector arr, int left, int right) { if (left >= right) return arr; int middlePoint = left + (right - left) / 2; //int middlePoint = arr.size() / 2; mergeSort(arr, left, middlePoint); mergeSort(arr, middlePoint + 1, right); return (merge(arr, left, middlePoint, right)); } int main() { string input; string element; vector numbers; // int arr1[] = { 12, 11, 13, 5, 6, 7 }; //int arr_size = sizeof(arr1) / sizeof(arr1[0]); //cout < arr_size;="" cout="">< "enter="" numbers:="" \n";="" getline(cin,="" input,="" '*');="" stringstream="" ss(input);="" while="" (getline(ss,="" element,="" ','))="" {="" numbers.push_back(stoi(element));="" }="" for="" (int="" i="0;" i="">< numbers.size();="" i++)="" {="" cout="">< numbers[i];="" }=""> result = mergeSort(numbers, 0, numbers.size() - 1); cout < "\n";="" for="" (int="" i="0;" i="">< result.size();="" i++)="" {="" cout=""><><" "; } } ";="" }="">
Answered Same DayApr 18, 2021

Answer To: // MergeSort.cpp : This file contains the 'main' function. Program execution begins and ends there....

Shashi Kant answered on Apr 18 2021
152 Votes
Merge/merge.cpp
#include
#include
#include
#include
usin
g namespace std;
void merge(vector &arr, int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0;
int j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] >= R[j]) {
arr[k] = L[i];
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here