c++ - Error with Merge Sort -
i trying create merge sort algorithm in c++ takes vector of generic type input , sorts elements in vector. except getting [run] bus error (core dumped) error everytime try test algorithm.
my code merge sort follows:
template <typename t> void merge_sort(std::vector<t>& vector) { typename std::vector<t>::iterator middle_value = vector.begin()+vector.size()/2; std::vector<t> left_side(vector.begin(), middle_value); std::vector<t> right_side(middle_value,vector.end()); merge_sort(left_side); merge_sort(right_side); std::vector<t> merged; int i,j; while(i<left_side.size() && j<right_side.size()) { if(left_side.at(i)<right_side.at(j)) { merged.push_back(left_side.at(i)); i++; } else { merged.push_back(right_side.at(j)); j++; } } while(i<left_side.size()) { merged.push_back(left_side.at(i)); i++; } while(j<right_side.size()) { merged.push_back(right_side.at(j)); j++; } for(int x=0;x<merged.size();x++) vector.at(x)=merged.at(x); }
any light on issue much.
Comments
Post a Comment