Skip to main content
Code Review

Return to Question

deleted 9 characters in body
Source Link
Arsen
  • 153
  • 5

I think this will help to avoid the 'while' loops at lines 17 - 29 when just concatenation of two sides is enough.

I think this will help to avoid the loops at lines 17 - 29 when just concatenation of two sides is enough.

I think this will help to avoid the 'while' loops when just concatenation of two sides is enough.

Remove noisy line numbers from code.
Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132
 5 typedef std::vector<int> int_v;
 6 
 7 int_v Merge(int_v& vec, int_v& l, int_v& r) {
 8  int_v res;
 9 res.reserve(l.size() + r.size());
 10  unsigned int li = 0;
 11  unsigned int ri = 0;
 12  if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 13  res.insert(res.end(), l.begin(), l.end());
 14  res.insert(res.end(), r.begin(), r.end());
 15  return res;
 16  }
 17  while (li < l.size() && ri < r.size()) {
 18  if (l[li] < r[ri]) {
 19  res.push_back(l[li++]);
 20  } else {
 21  res.push_back(r[ri++]);
 22  }
 23  }
 24  while (li < l.size()) {
 25  res.push_back(l[li++]);
 26  }
 27  while (ri < r.size()) {
 28  res.push_back(r[ri++]);
 29  }
 30  vec = res;
 31  return vec;
 32 }
 33 
 34 int_v MergeSort(int_v& v) {
 35  if (1 == v.size()) {
 36  return v;
 37  }
 38  int_v::iterator m = v.begin() + v.size()/2;
 39  int_v l(v.begin(), m);
 40  int_v r(m, v.end());
 41 
 42  l = MergeSort(l);
 43  r = MergeSort(r);
 44  return Merge(v, l, r);
 45 }
 9 res.reserve(l.size() + r.size()); 
 12  if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 13  res.insert(res.end(), l.begin(), l.end());
 14  res.insert(res.end(), r.begin(), r.end());
 15  return res;
 16  }
 5 typedef std::vector<int> int_v;
 6 
 7 int_v Merge(int_v& vec, int_v& l, int_v& r) {
 8  int_v res;
 9 res.reserve(l.size() + r.size());
 10  unsigned int li = 0;
 11  unsigned int ri = 0;
 12  if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 13  res.insert(res.end(), l.begin(), l.end());
 14  res.insert(res.end(), r.begin(), r.end());
 15  return res;
 16  }
 17  while (li < l.size() && ri < r.size()) {
 18  if (l[li] < r[ri]) {
 19  res.push_back(l[li++]);
 20  } else {
 21  res.push_back(r[ri++]);
 22  }
 23  }
 24  while (li < l.size()) {
 25  res.push_back(l[li++]);
 26  }
 27  while (ri < r.size()) {
 28  res.push_back(r[ri++]);
 29  }
 30  vec = res;
 31  return vec;
 32 }
 33 
 34 int_v MergeSort(int_v& v) {
 35  if (1 == v.size()) {
 36  return v;
 37  }
 38  int_v::iterator m = v.begin() + v.size()/2;
 39  int_v l(v.begin(), m);
 40  int_v r(m, v.end());
 41 
 42  l = MergeSort(l);
 43  r = MergeSort(r);
 44  return Merge(v, l, r);
 45 }
 9 res.reserve(l.size() + r.size()); 
 12  if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 13  res.insert(res.end(), l.begin(), l.end());
 14  res.insert(res.end(), r.begin(), r.end());
 15  return res;
 16  }
typedef std::vector<int> int_v;
int_v Merge(int_v& vec, int_v& l, int_v& r) {
 int_v res;
 res.reserve(l.size() + r.size());
 unsigned int li = 0;
 unsigned int ri = 0;
 if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 res.insert(res.end(), l.begin(), l.end());
 res.insert(res.end(), r.begin(), r.end());
 return res;
 }
 while (li < l.size() && ri < r.size()) {
 if (l[li] < r[ri]) {
 res.push_back(l[li++]);
 } else {
 res.push_back(r[ri++]);
 }
 }
 while (li < l.size()) {
 res.push_back(l[li++]);
 }
 while (ri < r.size()) {
 res.push_back(r[ri++]);
 }
 vec = res;
 return vec;
}
int_v MergeSort(int_v& v) {
 if (1 == v.size()) {
 return v;
 }
 int_v::iterator m = v.begin() + v.size()/2;
 int_v l(v.begin(), m);
 int_v r(m, v.end());
 l = MergeSort(l);
 r = MergeSort(r);
 return Merge(v, l, r);
}
res.reserve(l.size() + r.size()); 
 if (l.size() > 1 && (l[l.size()-1] < r[0])) {
 res.insert(res.end(), l.begin(), l.end());
 res.insert(res.end(), r.begin(), r.end());
 return res;
 }
deleted 57 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Merge Sort "improvement"?algorithm

I just read and experimented with c++C++ implementation of the merge sort algorithm from the Wikipedia page. During the experiments I have slightly modified the source code and would like to know how much the algorithm is improved (if it is).

Any and all critiques much appreciated.

Here is the original code:

time ./a.out 
real 0m26.278s
user 0m26.205s
sys 0m0.064s
time ./a.out 
real 0m26.278s
user 0m26.205s
sys 0m0.064s
time ./a.out 
real 0m22.129s
user 0m22.026s
sys 0m0.092s
time ./a.out 
real 0m22.129s
user 0m22.026s
sys 0m0.092s

Merge Sort "improvement"?

I just read and experimented with c++ implementation of the merge sort algorithm from the Wikipedia page. During the experiments I have slightly modified the source code and would like to know how much the algorithm is improved (if it is).

Any and all critiques much appreciated.

Here is the original code:

time ./a.out 
real 0m26.278s
user 0m26.205s
sys 0m0.064s
time ./a.out 
real 0m22.129s
user 0m22.026s
sys 0m0.092s

Merge Sort algorithm

I just read and experimented with C++ implementation of the merge sort algorithm from the Wikipedia page. During the experiments I have slightly modified the source code and would like to know how much the algorithm is improved (if it is).

time ./a.out 
real 0m26.278s
user 0m26.205s
sys 0m0.064s
time ./a.out 
real 0m22.129s
user 0m22.026s
sys 0m0.092s
Source Link
Arsen
  • 153
  • 5
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /