这个合并排序代码有什么问题?

what is wrong with this merge sort code?

我正在尝试使用迭代器编写合并排序来自学 C++,但出于某种原因,这段代码正在编译,但结果没有排序。有人能弄清楚它有什么问题吗?在我未经训练的眼睛看来完全没问题。

typedef vector<int> vec_int;
typedef vector<int>::iterator vec_int_iter;

void merge_sort(vec_int& vec, vec_int_iter low, vec_int_iter high){
   if(low < high){
      vec_int_iter med = low + (high-low)/2 ;
      merge_sort(vec, low, med);
      merge_sort(vec, med+1, high);
      arrange(vec, low, med, high);
      }
}

void arrange(vec_int& vec, vec_int_iter low, vec_int_iter med, vec_int_iter high){
   vec_int_iter left = low, right = med+1;
   vec_int temp;
   temp.clear();
   vec_int_iter it = temp.begin();

   while(left <= med and right <= high)
      temp.push_back( (*left < *right)? *left++ : *right++ );
   while(left <= med)
      temp.push_back( *left++ );
   while(right <= high)
      temp.push_back( *right++ );

   vec = temp;
}

错误的代码是vec = temp,它会在某些合并步骤中用临时向量替换原始向量。因为,每次排列,温度只是原向量从低到高。
然后原向量变成子向量。

你可以每次return一个新的向量,或者in place

示例代码,更改排列函数:

  void arrange(vec_int& vec, vec_int_iter low, vec_int_iter med, vec_int_iter high){
     vec_int_iter left = low, right = med+1;
     vec_int temp;
     temp.clear();
     while(left <= med and right <= high)
        temp.push_back( (*left < *right)? *left++ : *right++ );
     while(left <= med)
        temp.push_back( *left++ );
     while(right <= high)
        temp.push_back( *right++ );

     vec_int_iter start = low;
     for(vec_int_iter t = temp.begin(); t <temp.end(); t++){
        *start++ = *t;
     }   
  }