Skip to main content
small error
Source Link

I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays part and the function MERGE_SORT is the actual algorithm. I am interested to know whether or not my code has a good readability factor and if It can be optimized further or not?

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

#define itr vector<int>::iterator
void MERGE(vector<int>& vec,itr left, itr mid, itr right){
    //Make vector left with elements from left to mid inclusive
    vector<int> left_vec;
    for (itr i=left; i!=vec.end()&&i<=mid;i++){
        left_vec.push_back(*i);
    }
    left_vec.push_back(numeric_limits<int>::max());//sentinel card
    //make vector right with elements from mid+1 to right inclusive
    vector<int> right_vec;
    for (itr i=mid+1; i!=vec.end() && i<=right; i++){
        right_vec.push_back(*i);
    }
    right_vec.push_back(numeric_limits<int>::max());//sentinel card
    //Now add them in a sorted manner to vector from left to right
    
    itr l=left_vec.begin(); itr r=right_vec.begin();
    for (itr vec_itr=left;vec_itr!=vec.end()&&vec_itr<=right;vec_itr++){
        if (*l<=*r){
            *vec_itr=*l;
            l++;
        }
        else{
            *vec_itr = *r;
            r++;
        }
    }
}
void MERGE_SORT(vector<int>& vec, itr left, itr right){
    if(left<right){
        itr mid = left + (right-left)/2;
        MERGE_SORT(vec,left,mid);
        MERGE_SORT(vec,mid+1,right);
        MERGE(vec, left,mid,right);
    }
    return;
}
int main() {
    int a[] = {10,8,9,7,6,3,3,9,15,4,3,2,1};
    vector<int> vec(a,a+13);
    MERGE_SORT(vec,vec.begin(),vec.end()-1);
    for (itr i=vec.begin();i!=vec.end();i++)cout << *i << " ";
    return 0;
}

I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays part and the function MERGE_SORT is the actual algorithm. I am interested to know whether or not my code has a good readability factor and if It can be optimized further or not?

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

#define itr vector<int>::iterator
void MERGE(vector<int>& vec,itr left, itr mid, itr right){
    //Make vector left with elements from left to mid inclusive
    vector<int> left_vec;
    for (itr i=left; i!=vec.end()&&i<=mid;i++){
        left_vec.push_back(*i);
    }
    left_vec.push_back(numeric_limits<int>::max());//sentinel card
    //make vector right with elements from mid+1 to right inclusive
    vector<int> right_vec;
    for (itr i=mid+1; i!=vec.end() && i<=right; i++){
        right_vec.push_back(*i);
    }
    right_vec.push_back(numeric_limits<int>::max());//sentinel card
    //Now add them in a sorted manner to vector from left to right
    
    itr l=left_vec.begin(); itr r=right_vec.begin();
    for (itr vec_itr=left;vec_itr!=vec.end()&&vec_itr<=right;vec_itr++){
        if (*l<=*r){
            *vec_itr=*l;
            l++;
        }
        else{
            *vec_itr = *r;
            r++;
        }
    }
}
void MERGE_SORT(vector<int>& vec, itr left, itr right){
    if(left<right){
        itr mid = left + (right-left)/2;
        MERGE_SORT(vec,left,mid);
        MERGE_SORT(vec,mid+1,right);
        MERGE(vec, left,mid,right);
    }
    return;
}
int main() {
    int a[] = {10,8,9,7,6,3,3,9,15,4,3,2,1};
    vector<int> vec(a,a+13);
    MERGE_SORT(vec,vec.begin(),vec.end()-1);
    for (itr i=vec.begin();i!=vec.end();i++)cout << *i << " ";
    return 0;
}

I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays part and the function MERGE_SORT is the actual algorithm. I am interested to know whether or not my code has a good readability factor and if It can be optimized further or not?

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

#define itr vector<int>::iterator
void MERGE(vector<int>& vec,itr left, itr mid, itr right){
    //Make vector left with elements from left to mid inclusive
    vector<int> left_vec;
    for (itr i=left; i!=vec.end()&&i<=mid;i++){
        left_vec.push_back(*i);
    }
    left_vec.push_back(numeric_limits<int>::max());//sentinel card
    //make vector right with elements from mid+1 to right inclusive
    vector<int> right_vec;
    for (itr i=mid+1; i!=vec.end() && i<=right; i++){
        right_vec.push_back(*i);
    }
    right_vec.push_back(numeric_limits<int>::max());//sentinel card
    //Now add them in a sorted manner to vector from left to right
    
    itr l=left_vec.begin(); itr r=right_vec.begin();
    for (itr vec_itr=left;vec_itr!=vec.end()&&vec_itr<=right;vec_itr++){
        if (*l<=*r){
            *vec_itr=*l;
            l++;
        }
        else{
            *vec_itr = *r;
            r++;
        }
    }
}
void MERGE_SORT(vector<int>& vec, itr left, itr right){
    if(left<right){
        itr mid = left + (right-left)/2;
        MERGE_SORT(vec,left,mid);
        MERGE_SORT(vec,mid+1,right);
        MERGE(vec, left,mid,right);
    }
    return;
}
int main() {
    int a[] = {10,8,9,7,6,3,3,9,15,4,3,2,1};
    vector<int> vec(a,a+13);
    MERGE_SORT(vec,vec.begin(),vec.end());
    for (itr i=vec.begin();i!=vec.end();i++)cout << *i << " ";
    return 0;
}
Source Link

Recursive Implementation of Merge Sort C++

I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays part and the function MERGE_SORT is the actual algorithm. I am interested to know whether or not my code has a good readability factor and if It can be optimized further or not?

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

#define itr vector<int>::iterator
void MERGE(vector<int>& vec,itr left, itr mid, itr right){
    //Make vector left with elements from left to mid inclusive
    vector<int> left_vec;
    for (itr i=left; i!=vec.end()&&i<=mid;i++){
        left_vec.push_back(*i);
    }
    left_vec.push_back(numeric_limits<int>::max());//sentinel card
    //make vector right with elements from mid+1 to right inclusive
    vector<int> right_vec;
    for (itr i=mid+1; i!=vec.end() && i<=right; i++){
        right_vec.push_back(*i);
    }
    right_vec.push_back(numeric_limits<int>::max());//sentinel card
    //Now add them in a sorted manner to vector from left to right
    
    itr l=left_vec.begin(); itr r=right_vec.begin();
    for (itr vec_itr=left;vec_itr!=vec.end()&&vec_itr<=right;vec_itr++){
        if (*l<=*r){
            *vec_itr=*l;
            l++;
        }
        else{
            *vec_itr = *r;
            r++;
        }
    }
}
void MERGE_SORT(vector<int>& vec, itr left, itr right){
    if(left<right){
        itr mid = left + (right-left)/2;
        MERGE_SORT(vec,left,mid);
        MERGE_SORT(vec,mid+1,right);
        MERGE(vec, left,mid,right);
    }
    return;
}
int main() {
    int a[] = {10,8,9,7,6,3,3,9,15,4,3,2,1};
    vector<int> vec(a,a+13);
    MERGE_SORT(vec,vec.begin(),vec.end()-1);
    for (itr i=vec.begin();i!=vec.end();i++)cout << *i << " ";
    return 0;
}