快速排序
来源:互联网 发布:java 电商平台 编辑:程序博客网 时间:2024/04/30 04:51
找了一下以前的帖子,竟没有,补一下。
/home/a/j/nomad2:cat quicksort.c #include <stdio.h>int partition(int *a, int m, int n){ int low = m; int high = n; int pivot = low; int pvalue = a[pivot]; while(low < high) { while(low < high && a[high] >= pvalue) { high--; } a[low] = a[high]; while(low < high && a[low] <= pvalue) { low++; } a[high] = a[low]; } a[low] = pvalue; return low;}void quicksort(int *a, int m, int n){ int pivot; if ( m < n ) { pivot = partition(a,m,n); quicksort(a, m, pivot-1); quicksort(a, pivot+1, n); }}int main(){ int a[] = {49, 38, 65, 97, 76, 13, 27, 49}; int n = sizeof(a)/sizeof(int); int i = 0; quicksort(a, 0, n-1); for(i = 0; i < n; i++) { printf("%d ",a[i]); } printf("\n");}
运行结果:
/home/a/j/nomad2:./a.out 13 27 38 49 49 65 76 97
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- windows phone 7 instance: ruler
- Linux3.0.6内核task_struct注释
- .NET 定时执行写日志任务解决方案(Timer & Quartz.Net)
- ios多核心 处理代码
- wordpress theme 基本教程1
- 快速排序
- 30岁面霸IT男30次面试失败经历
- android layout属性介绍
- 调试PLC心得
- Android2.3 编译出错:No rule to make InterpAsm-x86.S
- 起点
- jsp登录实例
- js函数闭包
- ajax form 比较完美的应用