#include <iostream> using namespace std; void QuickSort(int a[], int low, int high)//0--- n-1 { int i, j, flagNum; if(low < high) { i = low; j = high; flagNum = a[low]; while(i < j) { while(i < j && a[j] >= flagNum) j--; if(i < j) a[i++] = a[j]; while(i < j && a[i] <= flagNum) i++; if(i < j) a[j--] = a[i]; } a[j] = flagNum; QuickSort(a, low, j-1); QuickSort(a, j+1, high); } } void bubbleSort(int a[], int n) { for(int i = 0; i <= n;i++) { for(int j = n; j > i ;j--) { if(a[j] <= a[j-1]) { int temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; } } } } int InsertSort(int a[], int n) { int i, j,temp; 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; } } int SelectSort(int a[], int n) { int i, j; for(i = 0;i <= n; i++) { for(j = i+1; j <= n; j++) { if(a[i] >= a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } int ShellSort(int a[], int n) { int i, h, j, temp; for(h = n/2; h > 0; h = h/2) { for(i = h; i <= n; i++) { temp = a[i]; for(j = i-h; j >=0 && temp <= a[j]; j = j-h) { a[j+h] = a[j]; } a[j+h] = temp; } } } int main() { int a[10] = {1, 2, 5, 3, 4, 6, 8, 7, 10, 9}; //QuickSort(a, 0, 9); //bubbleSort(a, 9); //InsertSort(a, 9); //SelectSort(a, 9); ShellSort(a, 9); for(int i = 0; i < 10; i++) { cout << a[i] << " "; } cout <<endl; return 0; }
天道酬勤!