#include<iostream> using namespace std; void Print(int arr[], int length) { for (int i = 0; i < length; i++) { cout << arr[i] << " "; } cout << endl; } int partition(int a[], int low, int height) { cout << "low: " << low << " height: " << height << endl; //完成之后,a[low]元素左侧就全部都是比它大的,右侧都是比它小的 while (low<height) { //从最开始的元素开始,从后往前找,找到第一个比它小的,交换 while (a[low]>a[height]) height--; swap(a[low], a[height]); //从开始往后找,找到第一个比它大的,交换 while (a[height] < a[low]) low++; swap(a[low], a[height]); } Print(a,7); cout << "privotLoc: " << low<<endl; //返回当前最低的那个元素排序后的位置 return low; } void Quick(int a[], int low, int height) { if (low >= height) return; int privotLoc = partition(a, low, height); Quick(a, low, privotLoc-1); Quick(a, privotLoc+1, height); } void Bubble(int a[], int length) { for (int i = 0; i < length; i++) { for (int j = i; j < length; j++) { if (a[i] > a[j]) { int t = a[j]; a[j] = a[i]; a[i] = t; } } } cout << "冒泡:"; Print(a, 7); cout << endl; } //将数组a[i,m-1]和a[m,n] 归并到b[i,n] void Merge(int a[], int b[], int i,int m,int n) { int t = i, k = m;//第一组和第二组的计数器 int j = i;//数组b的计数器 //遍历第二组内数据,若第一组内有比当前值小的,将第一组数放到数组b内,否则,将自己放入数组b while (k<n+1) { while(a[t] <a[k]&&t<n) b[j++] = a[t++]; b[j++] = a[k++]; } //遍历第一组剩余数据,将其放入数组b while (t<m) b[j++] = a[t++]; for (; i <= n; i++) { cout << b[i] << " "; } cout << endl; } void MergeSort(int a[],int b[],int length) { int len = 1; while (len<length) { //将数组分为长度为len个等长组,进行合并 int i = 0; while (i + 2 * len<length) { Merge(a, b, i, i + len, i + 2 * len - 1); i+=2 * len; } //对结尾不等长的组进行合并 if (i + len <= length) { Merge(a, b, i, i + len, length - 1); } //交换a和b,保证下次还是从a归并到b swap(a, b); //单个小组的长度增倍 len *= 2; } } void Selection(int a[], int length) { for (int i = 0; i < length; i++) { int max=a[i], key=i; for (int j = i; j < length; j++) { if (a[j] > max) { max = a[j]; key = j; } } swap(a[i], a[key]); } cout <<endl<< "选择:"; Print(a, length); } //i是加到末尾的子节点 void MinHeapFixup(int a[], int i) { //建二叉堆,父节点总比子节点小 for (int j = (i - 1) / 2; j >= 0 && a[j] < a[i]; i = j, j = (j - 1) / 2) swap(a[i], a[j]); } void MinHeapFixDown(int a[],int length) { int temp,i, j; temp = a[0]; i = 0; j = 2 * i + 1; while (j<length) { //1.左右孩子都要找,找到两个孩子中大的那个 if (j + 1 < length&&a[j + 1] > a[j]) j++; if (a[j] < a[i]) break; a[i] = a[j]; i = j; j = 2 * i + 1; } a[i] = temp; } //堆只能删除顶节点,删除之后还要修复 void MinHeapDelete(int a[],int length) { swap(a[0], a[length - 1]); MinHeapFixDown(a, length - 1); } void HeapSort(int a[], int length) { cout << "建堆" << endl; for (int i = 0; i < length; i++) { MinHeapFixup(a, i); Print(a, length); } cout << endl; cout << "排序" << endl; for (int i = 0; i < length; i++) { MinHeapDelete(a, length - i); Print(a, length); } cout << endl; Print(a, length); } void main() { int arr[7] = { 1, 5, 6, 16, 15, 122, 76 }; int b[7]; Print(arr, 7); ////先写个冒泡 //Bubble(arr, 7); ////块排 //Quick(arr, 0,6); //cout << endl << endl; ////归并 //MergeSort(arr, b, 7); ////选择 //Selection(arr, 7); //堆排序之前,首先认识到什么是堆,然后建立堆, //建完堆,之后,才可以对堆进行排序 HeapSort(arr, 7); Print(arr, 7); int a = sizeof(arr); cin >> a; }
参考:
http://blog.csdn.net/hguisu/article/details/7776068
http://blog.csdn.net/morewindows/article/details/6709644
http://www.blogjava.net/todayx-org/archive/2012/01/08/368091.html
待续
时间: 2024-10-04 23:15:45