博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
排序练习
阅读量:6060 次
发布时间:2019-06-20

本文共 4587 字,大约阅读时间需要 15 分钟。

 

#include 
/*二分非递归*/int HalfSearch(int a[], int low, int high, int key){ while(low <= high) { int mid = (low + high) / 2; if(a[mid] > key) { high = mid - 1; } else if(a[mid] < key) { low = mid + 1; } else { return mid; } }}/*二分递归*/int HalfSearch2(int a[], int low, int high, int key){ int mid = (low + high) / 2; if (low >= high) { return mid; } if (a[mid] > key) mid = HalfSearch2(a, low, mid - 1, key); else if(a[mid] < key) mid = HalfSearch2(a, mid + 1, high, key);}/*选择排序*/void SelectSort(int a[], int n){ int i, j, index, temp; for (i = 0; i < n - 1; i++) { index = i; for (j = i + 1; j < n; j++) { if (a[j] < a[index]) { index = j; } } if (index != i) { temp = a[index]; a[index] = a[i]; a[i] = temp; } }}/*插入排序*/void InsertSort(int a[], int n){ int i, j, temp; for(i = 1; i < n; i ++) { temp = a[i]; if(temp < a[i - 1]) { for(j = i - 1; (j >= 0) && (temp < a[j]); j--) { a[j + 1] = a[j]; a[j] = temp; } } }}/*冒泡排序*/void BubbleSort(int a[], int n){ int i, j, temp; for(i = 0; i < n; i ++) { for(j = i + 1; j < n - 1; j ++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } }}/*快速排序*/void QuickSort(int a[], int low, int high){ int i = low; int j = high; int temp = a[low]; if (low > high) { return; } while (i < j) { while((i < j) && (a[j] > temp)) { j--; } if (i < j) { a[i] = a[j]; i++; } while((i < j) && (a[i] < temp)) { i++; } if (i < j) { a[j] = a[i]; j--; } } a[i] = temp; QuickSort(a, low, i - 1); QuickSort(a, i + 1, high);}/*归并合并*/void MergeArray(int a[], int low, int mid, int high, int temp[]){ int t = 0; int k = 0; int i = low; int m = mid; int n = mid + 1; int j = high; while ((i <= m) && (n <= j)) { if (a[i] < a[n]) { temp[t++] = a[i++]; } else if (a[i] > a[n]) { temp[t++] = a[n++]; } } while(i <= m) { temp[t++] = a[i++]; } while(n <= j) { temp[t++] = a[n++]; } for (k = 0; k < t; k++) { a[low + k] = temp[k]; }}/*归并排序*/void MergeSort(int a[], int low, int high, int temp[]){ if (low < high) { int mid = (low + high)/2; MergeSort(a, low, mid, temp); MergeSort(a, mid + 1, high, temp); MergeArray(a, low, mid, high, temp); }}/*堆增加节点*/void HeapInsert(int a[], int n, int data){ a[n] = data; int i = n; while ((i/2 > 0) && (a[i] > a[i/2])) { int temp = a[i]; a[i] = a[i/2]; a[i/2] = temp; i = i/2; }}/*从上到下处理堆节点*/void HeapHandler(int a[], int n, int i){ while (1) { int max = i; if (((i*2) <= n) && (a[i] < a[i*2])) { max = i * 2; } if (((i*2 + 1) <= n) && (a[max] < a[i*2 + 1])) { max = i * 2 + 1; } if (max == i) { break; } int temp = a[i]; a[i] = a[max]; a[max] = temp; i = max; }}/*堆删除*/void HeapDelete(int a[], int n){ a[1] = a[n]; n = n - 1; HeapHandler(a, n, 1);}/*创建堆*/void HeapCreat(int a[], int n){ int i; for(i = (n/2); i >= 1; i--) { HeapHandler(a, n, i); } // int k; // for (k = 0; k <= n; k++) // { // printf("a[%d]:%d\n", k, a[k]); // }}/*堆排序*/void HeapSort(int a[], int n){ HeapCreat(a, n); int i = n; while (i > 1) { int temp = a[i]; a[i] = a[1]; a[1] = temp; i = i - 1; HeapHandler(a, i, 1); }}int main(){ int i = 0; int a[10] = {
2, 9, 4, 1, 3, 55, 46, 15, 10, 32}; int b[10] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int c[10]; int d[11] = {
0, 2, 9, 4, 1, 3, 55, 46, 15, 10, 32}; //MergeSort(a, 0, 9, c); HeapSort(d, 10); for (i = 0; i < 11; i++) { printf("d[%d]:%d\n", i, d[i]); } //printf("%d\n", HalfSearch2(b, 0, 9, 7)); return 0;}

 

堆排序参考:

转载于:https://www.cnblogs.com/zzdbullet/p/10462082.html

你可能感兴趣的文章
js 操作数组函数-自定义
查看>>
js 删除 根据不同情况,弹出不同窗口。
查看>>
CentOS 5.5下FTP安装及配置_百度文库 vsftpd
查看>>
学习不同类型的知识要用不同的方法
查看>>
深入医生orm之insert细节
查看>>
ExtJs之Ext.data.Store
查看>>
时间选择器 可以选择日期和时间
查看>>
查询BLOB字段的长度
查看>>
让你的网页文本框增加光晕效果与提示,水印(类似QQ2011)
查看>>
【sas sql proc】case end
查看>>
myeclipes启动tomcat6报错解决方案:a configuration error occ
查看>>
xss攻击
查看>>
每日英语:America's Baby Bust
查看>>
来自 王斌 (@iwangbin) 的推文
查看>>
ORA-00600:[1112]内部错误&ROW CACHE ENQUEUE LOCK一例
查看>>
NDK提供的共享库(Prebuilt)
查看>>
双口RAM防冲突方法(转)
查看>>
加载NT驱动
查看>>
Caused by: java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory
查看>>
配置 POSTGRESQL 远程访问
查看>>