各种排序算法
来源:互联网 发布:云代账软件收费吗 编辑:程序博客网 时间:2024/05/17 21:48
年代有些久远了,但平时排序算法用得很多,所以汇总一下
各排序算法复杂度和稳定性的比较
C或c++实现
直接插入
void insert(int vector[],int n) { // 直接插入算法 int i, j, t; for (i=1; i<n; i++) { t=vector[i]; for(j=i-1; j>=0 && t<vector[j]; j--) { vector[j+1] = vector[j]; } vector[j+1] = t; }}
直接选择
Void select(int a[], int n) { // 直接选择算法 int i, j; for(i = 0; i < n-1; i++) { k=i; for (j = i+1; j < n; j++) if(a[k] > a[j]) k = j; if(i != k) swap(a[k], a[i]); }}
冒泡排序
void bubble_sort(int a[],int n) { // 冒泡排序算法: int i, j; for (i = 0; i < n-1; i++) { for (j = 0; j < n-1-i; j++) if (a[j] > a[j+1]) swap(a[j], a[j+1]) }}
shell排序
// Shell 排序void shellSort(int array[], int length) { int p, i, j, t; for (p = length/2; p > 0; p /= 2) { for (i = p; i < length; i++) { for(j = i-p; j >= 0 && array[j] > array[j+p]; j -= p) { t = array[j]; array[j] = array[j+p]; array[j+p] = t; } } }}
void sort(int*a, int i, int j) {int left, right, mid, t;left = i; right = j;mid = a[(i+j)/2];do {while (a[left] < mid) left++;while (a[right] > mid) right--;if (left <= right) {t = a[left]; a[left] = a[right]; a[right] = t;left++; right--;}} while (left <= right);if (left < j) sort(a, left, j);if (right > i) sort(a, i, right);}
堆排序
#include<iostream>using namespace std;void Swap(int &a, int &b) {int t = a;a = b;b = t;}void HeapAdjust(int s[], int start, int num) { // 调整为最大堆 int i, j;while (2*start+1 < num) {j = 2*start+1;if (j+1 < num && s[j] < s[j+1]) j++; // 选择左右子树中的较大者与父节点交换if (s[start] < s[j]) {Swap(s[start], s[j]);start = j; // 反复筛选 }else break; }} void HeapSort(int s[], int n) {int i, j;for (i = n/2-1; i >= 0; i--) HeapAdjust(s, i, n); // 初始化最大堆for (j = n-1; j >= 0; j--) {Swap(s[0], s[j]); // 通过交换堆顶元素与堆底元素达到取堆顶最大元素的目的 HeapAdjust(s, 0, j); // 对剩余j-1元素重新建成堆调整} }int main() {int a[12] = { 8, 2, 5, 43, 54, 1, 88, 23, 15, 99, 24, 33 };HeapSort(a, 12);for (int p = 0; p < 12; p++) cout << a[p] << " ";cout << endl;system("pause");return 0;}
归并排序
无递归:
#include<iostream>#include<cstdio>using namespace std;void MergeStep(int a[], int r[], int s, int m, int n) {int i = s, k = s, j = m+1;while (i <= m &&j <= n) {if (a[i] <= a[j]) r[k++] = a[i++];else r[k++] = a[j++];}while (i <= m) r[k++] = a[i++];while (j <= n) r[k++] = a[j++];}void MergePass(int a[], int r[], int n, int len) {int s = 0, e;while (s + len < n) {e = s + 2 * len - 1;if (e >= n) e = n - 1;MergeStep(a, r, s, s + len - 1, e);s = e + 1;}if (s < n) {for (; s < n; s++) r[s] = a[s];}}void MergeSort(int a[], int n) {int *p;int len = 1, f = 0;p = (int*)malloc(sizeof(int)*n);while (len < n) {if (f) MergePass(p, a, n, len);else MergePass(a, p, n, len);len *= 2;f = 1 - f;}if (f) {for (f = 0; f < n; f++) a[f] = p[f];}free(p);}int main() {int a[12] = { 8, 2, 5, 43, 54, 1, 88, 23, 15, 99, 24, 33 };MergeSort(a, 12);for (int p = 0; p < 12; p++) cout << a[p] << " ";cout << endl;system("pause");return 0;}
有递归:
void Merge(int a[], int low, int m, int high) {// low为第1有序区的首元素, m为第1有序区的末元素 int *b = new int[high-low+1];if (!b) {cout << "ERROR!" << endl;return; }int i = low, k = 0, j = m+1; // j(m+1)为第2有序区的首元素 while (i <= m && j <= high) {if (a[i] <= a[j]) b[k++] = a[i++];else b[k++] = a[j++];}while (i <= m) b[k++] = a[i++];while (j <= high) b[k++] = a[j++];for(i=low, k=0; i <= high; i++, k++) a[i]=b[k]; // //将排好序的存回数组a中low到high这区间 delete []b;}void Msort(int a[], int s, int t) {int m = (s+t) /2;if (s < t) {Msort(a, s, m); // 左边 Msort(a, m+1, t); // 右边 Merge(a, s, m, t);}}
0 0
- 【算法】各种排序算法
- 各种排序算法
- 各种排序算法
- 各种排序算法收集
- 各种排序算法
- 各种排序算法小结
- 各种排序算法小结
- 各种内部排序算法!
- 各种排序算法小结
- 各种排序算法
- 各种排序算法
- 各种排序算法介绍
- 各种排序算法
- 各种排序算法比较
- 各种排序算法
- 各种排序算法
- C++各种排序算法
- 各种排序算法
- 数组中重复的数字
- Nessus漏洞扫描教程之安装Nessus工具
- vao对象及其使用
- 存储过程
- java创建线程的三种方式及其对比
- 各种排序算法
- string和stringBuilder的区别
- Webform的上传图片
- 折线图
- 利用mysql查询时遇到的语法错误
- 'inline' is the only legal storage class for constructors
- Chart图表
- Android面试之Activity生命周期
- HX711简介