1. 程式人生 > >採用vector實現快排,堆排序,歸併排序

採用vector實現快排,堆排序,歸併排序

半路出家,學習ing......

快排:

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
void swap(T *i, T *j){
  T *temp;
  temp = j;
  j = i;
  i = temp;
}

// increase
template <typename T>
void quicksort(vector<T> &invec, int &left, int &right){
 int i = left;
 int j = right;
 T mid = invec[(i+j+1)/2];
 while(i <= j){
   while(invec[i] < mid){
     ++i;
  }
  while(invec[j] > mid){
    --j;
  }
  if(i<=j){
    swap(invec[i], invec[j]);
    ++i;
    --j;
  }
  if(i<right){
    quicksort(invec, i, right);
  }
  if(j>left){
    quicksort(invec, left, j);
  }
 }
}

// decrease
void quicksort2(vector<int> &invec, int &left, int &right){
  int i = left;
  int j = right;
  int mid = invec[(i+j)/2];
  while(i<=j){
    while(invec[i]>mid){
      ++i;
    }
    while(invec[j]<mid){
      --j;
    }
    if(i<=j){
      swap(invec[i],invec[j]);
      ++i;
      --j;
    }
    if(i<right){
      quicksort2(invec, i, right);
    }
    if(j>left){
      quicksort2(invec, left, j);
    }
  }
}

int main(){
  vector<int> invec{1, 2, 3, 7, 5, 9, 0, 11, 15, 8, 30, 20, 19};
  //vector<char> invec{'a','v','t','b','z','c'}; // output: a b c t v z 
  //vector<string> invec{"abc", "abd", "abda", "aacdw"}; // output: aacdw abc abd abda
//   int temp;
//   while(cin >> temp){
//     
//     invec.push_back(temp);
//   }
  int start = 0;
  int end = invec.size()-1;
  quicksort(invec, start, end);//0 1 2 3 5 7 8 9 11 15 19 20 30
  //quicksort2(invec, start, end);//30 20 19 15 11 9 8 7 5 3 2 1 0
  for(const auto &i:invec)
    cout << i << " ";
  cout << endl;
}

堆排序:

#include <iostream>
#include <vector>

using namespace std;

void printkk(vector<int> &invec, int &len){
  for(int i = 0; i < len; ++i)
    cout << invec[i] << " ";
}

void swap(int &a, int &b){
  int temp;
  temp = b;
  b = a;
  a = temp;
}

// heap sorting
void Heapify(vector<int> &invec, int &s, int &len){
  int fa = s;
  int child = 2*fa+1;
  cout << "fa1 = " << fa <<" " << "child1 = " << child << endl;
  while(child < len){
    if(child+1 < len && invec[child] < invec[child+1]){
      ++child;
    }
    if(invec[fa]<invec[child]){
      swap(invec[fa], invec[child]);
      fa = child;
      child = 2*fa+1;
      cout << "fa2 = " << fa <<" " << "child2 = " << child << endl;
    }
    else{
      break;
    }
  }
  printkk(invec,len);
  cout << endl;
}

int start = 0;

void HeapSort(vector<int> &invec, int &len){
  // initial building heap
  for(int i = (len-1)/2; i>=0;--i){
    Heapify(invec, i, len);
  }
  for(int i = len-1; i>0;--i){
    swap(invec[i],invec[start]);
    Heapify(invec, start, i);
  }
}

int main(){
  vector<int> invec{1, 2, 3, 7, 5, 9, 0, 11, 15, 8, 30, 20, 19};
  int len = invec.size();
  HeapSort(invec, len);
  printkk(invec, len);
  cout << endl;
}

歸併排序:

#include <iostream>
#include <vector>

using namespace std;

void swap(int &a, int &b){
  int temp;
  temp = b;
  b = a;
  a = temp;
}

void Merge(vector<int> &invec, int &start, int &ending, vector<int> &res){
  int is2 = start;
  int ie2 = ending;
  int left_index = is2;
  int left_len = (ie2+is2+1)/2;
  int right_index = is2+left_len;
  int res_index = is2;
  while((left_index < (is2+ left_len)) && (right_index < ie2+1)){
    res[res_index++] = (invec[left_index] < invec[right_index]) ? invec[left_index++] : invec[right_index++];
  }
  while(left_index < (is2+ left_len)){
    res[res_index++] = invec[left_index++];
  }
  while(right_index <= ie2){
    res[res_index++] = invec[right_index++];
  }
//   for(int i =is2; i<ie2;++i){
//     invec[i] = res[i];
//   }
}

void mergeSort(vector<int> &invec, int &start, int &ending, vector<int> &res){
  int is = start;
  int ie = ending;
  int mid = (is+ie+1)*0.5;
  int midr = mid+1;
  if(1 == ie-is){
    if(invec[is] > invec[ie]){
      swap(invec[is], invec[ie]);
      res[is] = invec[is];
      res[ie] = invec[ie];
    }
    return;
  }
  else if(0 == ie-is){
    return;
  }
  else{
    mergeSort(invec, is, mid, res); // the left region
    mergeSort(invec, midr, ie, res); // the right region
    
    Merge(invec, is, ie, res);
//     for(int i = is;i<ie;++i){
//       invec[i] = res[i];
//     } 
  }
}

int main(){
  vector<int> invec{1, 2, 3, 7, 5, 9, 0, 11, 15, 8, 30, 20, 19};
  //vector<int> *p = new int[]{1, 2, 3, 7, 5, 9, 0, 11, 15, 8, 30, 20, 19};
  int start = 0;
  int length = invec.size();
  int len = invec.size()-1;
  vector<int> res;
  res.resize(length);
  mergeSort(invec, start, len, res);
  
  for(auto &i: res){
    cout << i << " ";
  }
  cout << endl;
}