要开始准备找工作了,昨天闲时就自己写了个数据结构排序类,包括了堆排序,归并排序,速度排序,插入排序,正在继续完善中......
SortAlgorithm.h 代码如下:
#pragma once
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class SortAlgorithm
{
public:
SortAlgorithm(void);
~SortAlgorithm(void);
void heapsort(vector<int> &n);
void mergesort(vector<int> &n);
void quicksort(vector<int> &n);
void insertsort(vector<int> &n);
private:
void Down(vector<int> &n, int hole, int L);
void mergeSort(vector<int> &n, vector<int> &tmp, int s, int e);
void merge(vector<int> &n, vector<int> &tmp, int s, int mid, int e);
void quickSort(vector<int> &n, int s, int e);
int getMidVal(vector<int> &n, int s, int e);
void insertSort(vector<int> &n, int s, int e);
};
SortAlogrithm.cpp 代码如下:
#include "SortAlgorithm.h"
SortAlgorithm::SortAlgorithm(void)
{
}
SortAlgorithm::~SortAlgorithm(void)
{
}
/* ----------------------------------堆排序 过程 ---------------------------------------------*/
void SortAlgorithm::heapsort(vector<int> &n)
{
for(int i = n.size()/2; i>= 0; i--) { /* build Max-Heap */
Down(n,i,n.size());
}
for(int i = n.size()-1; i > 0; i--)
{
swap(n[0],n[i]); // 删除最大值
Down(n,0,i);
}
}
//下移过程
void SortAlgorithm::Down(std::vector<int> &a, int hole, int L)
{
int Left = 2*hole+1;
int val = a[hole];
while(Left< L)
{
if(Left+1<L && a[Left+1] > a[Left] && val < a[Left+1]) {
a[hole] = a[Left+1];
hole = Left+1;
}
else if(val < a[Left]){
a[hole]=a[Left];
hole = Left;
}
else break;
Left = 2*hole+1;
}
a[hole] = val;
}
/* ----------------------------------堆排序 过程 结束-----------------------------------------*/
/* ----------------------------------归并排序 过程 ---------------------------------------------*/
void SortAlgorithm::mergesort(vector<int> &n)
{
vector<int> tmp(n.size());
mergeSort(n,tmp,0,n.size()-1);
}
void ::SortAlgorithm::mergeSort(vector<int> &n, vector<int> &tmp, int s, int e)
{
if(s < e) {
int mid = (s+e)/2;
mergeSort(n,tmp,s,mid);
mergeSort(n,tmp,mid+1,e);
merge(n,tmp,s,mid+1,e);
}
}
void ::SortAlgorithm::merge(std::vector<int> &n, std::vector<int> &tmp, int s, int mid, int e)
{
int i = s, j = mid;
int t = s;
while(i< mid && j <= e)
{
if(n[i] < n[j]) tmp[t++] = n[i++];
else tmp[t++] = n[j++];
}
while(i< mid) tmp[t++] = n[i++];
while(j <= e) tmp[t++] = n[j++];
for(int k = s; k<= e; ++k) n[k] = tmp[k];
}
/* ----------------------------------归并排序 过程 结束-----------------------------------------*/
/* ----------------------------------快速排序 过程 ---------------------------------------------*/
void SortAlgorithm::quicksort(vector<int> &n)
{
quickSort(n,0,n.size()-1);
}
void ::SortAlgorithm::quickSort(vector<int> &n, int s, int e)
{
if(s + 10 < e)
{
int mval = getMidVal(n,s,e);
int i = s;
int j = e;
while(true)
{
while(n[++i] < mval) ;
while(n[--j] > mval) ;
if(i< j) swap(n[i],n[j]);
else break;
}
quickSort(n,s,i-1); // sort left-small array
quickSort(n,j+1,e); // sort right-large array
}
else insertSort(n,s,e);
}
int ::SortAlgorithm::getMidVal(vector<int> &n, int s, int e) // get the Mid-Val to divide
{
int mid = (s+e)/2;
if(n[mid] < n[s]) swap(n[mid],n[s]);
if(n[e] < n[s]) swap(n[e],n[s]);
if(n[e] < n[mid]) swap(n[e],n[mid]);
swap(n[mid],n[e-1]);
return n[e-1];
}
/* ----------------------------------快速排序 过程 结束-----------------------------------------*/
/* ----------------------------------插入排序 过程 ---------------------------------------------*/
void SortAlgorithm::insertsort(vector<int> &n)
{
insertSort(n,0,n.size()-1);
}
void ::SortAlgorithm::insertSort(vector<int> &n, int s, int e)
{
for(int i = s + 1; i<= e; ++i)
{
int v = n[i];
for(int j = i-1; j >= 0 && n[j] > v; --j) swap(n[j],n[j+1]);
}
}
/* ----------------------------------插入排序 过程 结束-----------------------------------------*/