#include <iostream> using namespace std; int main( ) { //普通插入排序 //void InsertSort(int a[], int n); //对普通插入排序改进的希尔排序 //void ShellSort(int a[], int n); //选择法排序 //void SelectSort(int a[], int n); //堆排序 //void HeapSort(int a[], int n); //冒泡排序 //void BubbleSort(int a[], int n); //改进过的冒泡算法 //void BubbleSort2(int a[], int n); //快速排序算法 //void QSort(int a[], int low, int high); //封装了快速排序算法QSort void QuickSort(int a[], int n); int a[10] = {0,1,2,3,9,4,5,6,8,7}; for(int i=0; i<10; ++i) { cout << a[i] << " "; } cout << endl; //InsertSort(a, 10); //ShellSort(a, 10); //SelectSort(a, 10); //HeapSort(a, 10); //BubbleSort(a, 10); //BubbleSort2(a, 10); //QSort(a, 0, 9); QuickSort(a, 10); for(i=0; i<10; ++i) { cout << a[i] << " "; } cout << endl; return 0; } //普通插入排序 void InsertSort(int a[], int n) { int i = 0; int j = 0; int temp = 0; for (i=1; i<n; ++i) { temp = a[i]; for (j=i-1; j>=0 && temp<a[j]; --j) { a[j+1] = a[j]; } a[j+1] = temp; } } //对普通插入排序进行改进后的希尔排序 void ShellSort(int a[], int n) { int i = 0; int j = 0; int temp = 0; int increment = n; do { increment = increment/3 + 1; for (i=increment; i<n; ++i) { temp = a[i]; for (j=i-increment; (j>=0)&&(temp<a[j]); j -= increment) { a[j+increment] = a[j]; } a[j+increment] = temp; } } while (increment > 1); } //选择法排序 void SelectSort(int a[],int n) { int temp = 0; for (int i=0; i<n-1; ++i) { for (int j=i+1; j<n; ++j) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } //堆排序开始------------------------------------------ //对选择法排序进行改进后的堆排序 void HeapSort(int a[], int n) { void HeapAdjust(int a[], int m, int n); int temp = 0; int i = n/2-1; for (i=n/2-1; i>=0; --i) { HeapAdjust(a, i, n-1); } for (i=n-1; i>0; --i) { temp = a[0]; a[0] = a[i]; a[i] = temp; HeapAdjust(a, 0, i-1); } } //将由m————n的序列调整成为大根堆,在这个序列中m+1————n满足大根堆特性 void HeapAdjust(int a[], int m, int n) { int temp = a[m]; int j = 0;//j表示待调整数的较大子节点的下标,默认左子节点最大 for (j=m*2+1; j<=n; j=j*2+1) { if ((j<n) && (a[j]<a[j+1]))//调整j,使之标识实际的最大子节点儿 { ++j; } if (temp >= a[j]) { break; } a[m] = a[j]; m = j; } a[m] = temp; } //堆排序结束------------------------------------------ //冒泡排序 void BubbleSort(int a[], int n) { int i = 0; int j = 0; int temp = 0; for (i=n-1; i>0; --i) { for (j=0; j<i; ++j) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } //改进过的冒泡算法 void BubbleSort2(int a[], int n) { int i = 0; int j = 0; int temp = 0; bool b = true; for (i=n-1; (i>0)&&b; --i) { b = false; for (j=0; j<i; ++j) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; b = true; } } } } //快速排序开始------------------------------------------------ //对快速排序进行封装,以时其与其它排序算法有一个相似的接口 void QuickSort(int a[], int n) { void QSort(int a[], int low, int high); QSort(a, 0, n-1); } //对冒泡算法改进过的快速排序算法 void QSort(int a[], int low, int high) { int partition(int a[], int low, int high); if (low < high) { int pivot = partition(a, low, high); QSort(a, low, pivot-1); QSort(a, pivot+1, high); } } //对划分函数partition的定义 int partition(int a[], int low, int high) { int pivotValue = a[low]; int temp = 0; while (low < high) { while ((low<high) && (a[high]>=pivotValue)) { --high; } temp = a[low]; a[low] = a[high]; a[high] = temp; while ((low<high) && (a[low]<=pivotValue)) { ++low; } temp = a[low]; a[low] = a[high]; a[high] = a[low]; } return low; } //快速排序结束------------------------------------------------