Q:

Merge Sort only returns vectors that have a size that is a power of 2

So I am trying to write code to get a merge sort work on vectors. However I now have a problem where whenever I enter a size of vector size that isn't a power of 2 the final sorted vector will round down its size to the closest power of 2. So e.g. if the vector size is set to 10 it'll return 8, if it's 100 it'll return 64. I don't know why this is. Here is the code:
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>  /* time */
#include <vector>
#include <assert.h>
#include <iterator>
#include <algorithm> 
#include <functional> 
using namespace std;

vector<int> merge(vector<int> &left, vector<int> &right) {
    vector <int> result;
    while (left.size() > 0 && right.size()>0) {
        if (left[0] <= right[0]) {
            result.push_back(left[0]);
            left.erase(left.begin());
        }
        else {
            result.push_back(right[0]);
            right.erase(right.begin());
        }
    }
    if (left.size() > 0) {
        result.insert(result.end(), left.begin(), left.end());
    }
    if (right.size() > 0) {
        result.insert(result.end(), right.begin(), right.end());
    }
    return result;
}

vector <int> mergesort(vector < int > &vector_sort) {
    int size_of = vector_sort.size() / 2;
    vector <int > left(size_of), right(size_of), result(vector_sort.size());
    if (vector_sort.size() <= 1) {
        return vector_sort;
    }
    else {
        int middle = vector_sort.size() / 2;
        int counter = middle;
        int second_half = middle;
        for (int i = 0; i < middle; i++) {
            left[i] = vector_sort[i];
        }
        for (int i = 0; i < middle; i++) {
            right[i] = vector_sort[counter];
            counter++;
        }
        left = mergesort(left);
        right = mergesort(right);
        if (left.back() <= right[0]) {
            left.insert(left.end(), right.begin(), right.end());
            return left;
        }
        result = merge(left, right);
        return result;
    }
}
int main() {
    const int t=10;
    vector<int> vector_no(t);
    vector<int> empty(t);
    for (int i = 0; i < t; i++) {
        int hello = rand() % 100000 + 1;
        vector_no[i] = hello;
    }
    int hello2 = 0;
    for (int i = 0; i < vector_no.size(); i++) {
        hello2++; //I am using this for loop here just to count the size of the vector without getting a stack overflow error
    }
    cout << hello2  << "\n";
    vector_no = mergesort(vector_no);
    hello2 = 0;
    for (int i = 0; i < vector_no.size(); i++) {
        hello2++; //I am using this for loop here just to count the size of the vector without getting a stack overflow error
    }
    cout << hello2 << "\n";
}

A:

When you construct the right vector, you are adding a number of elements equal to middle, when you should be adding vector_sort.size() - middle number of elements elements. Because division rounds down, this causes you to drop the last element on lists that are odd length. And because you merge sort down to 0 or 1 elements, it causes your results to be powers of 2.

