6种排序:
三种简单排序:插入排序、冒泡排序、选择排序
三种高级排序:希尔排序、快速排序、归并排序
案例:
未排序序列:
23 , 17 , 54 , 39 , 89 , 45 , 76 , 46 , 34 , 26
6种排序的结果:
17 , 23 , 26 , 34 , 39 , 45 , 46 , 54 , 76 , 89
运行结果:
源代码如下:
#include<iostream>
using namespace std;
const int M = 10; //数据最大容量
const int N = 10; //数据当下容量
class Sort{
public:
Sort(int m, int l = 0) :maxsize(m), listsize(l) { a = new int[maxsize]; }
~Sort() { delete[]a; }
bool append(int it);
void insertsort(); //简单插入排序
void bubblesort(); //冒泡排序
void selectsort(); //选择排序
void shellsort(); //希尔排序
void quicksort(); //快速排序
void mergesort(); //归并排序
void print();
private:
int* a,maxsize,listsize;
void Insertsort2(int start, int gap); //希尔排序辅助函数
void Qsort(int left, int right); //快速排序辅助函数
int Partition(int left, int right); //快速排序辅助函数
void Msort(int left, int right); //归并排序辅助函数
void Merge(int left, int middle, int right);//归并排序辅助函数
};
bool Sort::append(int it) {
if (listsize >= maxsize) return false;
a[listsize++] = it;
return true;
}
void Sort::insertsort() {
for ( int i = 1; i < listsize; i++) {
int temp = a[i];
int j = i - 1;
while (a[j] > temp ) {//退出结果:a[j]<=temp;表示其之后的位置才是temp应该插入的位置
a[j + 1] = a[j];
j--;
}
a[j+1] = temp;
}
}//简单插入排序
void Sort::bubblesort() {
for (int i = 0; i < listsize - 1; i++) {
bool flag = true;
for (int j = listsize - 1; j > i; j--) {
if (a[j - 1] > a[j]) { int temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; flag = false; }
}
if (flag == true) break;
}
}//冒泡排序
void Sort::selectsort() {
for (int i = 0; i < listsize-1; i++) {
int key=i;
for (int j = i + 1; j < listsize; j++)
if (a[j] < a[key]) key = j;
if (key != i) { int temp = a[i]; a[i] = a[key]; a[key] = temp; }
}
}//选择排序
void Sort::shellsort() {
for (int gap = listsize / 2; gap >= 2; gap /= 2)
for (int start = 0; start < gap; start++) Insertsort2(start, gap);
Insertsort2(0, 1);
}//希尔排序
void Sort::Insertsort2(int start, int gap) {
for (int i = start + gap; i < listsize; i += gap) {
int temp = a[i];
int j = i - gap;
while (a[j] > temp && j >= 0) {
a[j + gap] = a[j];
j -= gap;
}
a[j + gap] = temp;
}
}
void Sort::quicksort() {
Qsort(0, listsize - 1);
}//快速排序
void Sort::Qsort(int left, int right) {
if (left < right) {
int pivotpos = Partition(left, right);
Qsort(left, pivotpos - 1);
Qsort(pivotpos +1, right);
}
}
int Sort::Partition(int left, int right) {
int pivot = a[left];
while (left < right) {
while (left<right && a[right]>pivot) right--;
if (left < right) a[left] = a[right];
while (left < right && a[left] <= pivot) left++;
if (left < right) a[right] = a[left];
}
a[left] = pivot;
return left;
}
void Sort::mergesort() {
Msort(0, N - 1);
}//归并排序
void Sort::Msort( int left, int right) {
if (left == right) return;
int middle = (left + right) / 2;
Msort(left, middle);
Msort(middle + 1, right);
Merge(left, middle, right);
}
void Sort::Merge(int left, int middle,int right) {
int i = left, j = middle+1;
int k = left, b[N];
while (i <= middle && j <= right) {
if (a[i] <= a[j]) b[k++] = a[i++];
else b[k++] = a[j++];
}
while (i <= middle) b[k++] = a[i++];
while (j <= right)b[k++] = a[j++];
for (int t = left; t <= right; t++) a[t] = b[t];
}
void Sort::print() {
cout << "最大容量:" << maxsize << '\t' << "当下容量:" << listsize << endl;
for (int i = 0; i < listsize; i++) cout << a[i] << '\t';
cout << endl;
}
int main() {
Sort sort(M);
int a[M] = { 23,17,54,39,89,45,76,46,34,26 };
for (int i = 0; i < N; i++) sort.append(a[i]);
cout<<"初始数据如下:"<<endl;
sort.print();
/*cout<<"插入排序结果如下:"<<endl;
sort.insertsort();*/
/*cout<<"冒泡排序结果如下:"<<endl;
sort.bubblesort();*/
/*cout<<"选择排序结果如下:"<<endl;
sort.selectsort();*/
/*cout<<"希尔排序结果如下:"<<endl;
sort.shellsort();*/
/*cout<<"快速排序结果如下:"<<endl;
sort.quicksort();*/
cout << "归并排序结果如下:" << endl;
sort.mergesort();
sort.print();
return 0;
}