首页 > 代码库 > 快速排序
快速排序
- #include <cstdio>
- #define ARRLEN(x) (sizeof(x)/sizeof(int))
- int partition(int a[], int low, int high)
- {
- int pivotkey=a[low];
- while (low<high)
- {
- while (low<high && a[high]>=pivotkey)
- --high;
- if (low<high)
- a[low++]=a[high];
- while (low<high && a[low]<=pivotkey)
- ++low;
- if (low<high)
- a[high--]=a[low];
- }
- a[low]=pivotkey;
- return low;
- }
- void quicksort(int a[], int low, int high)
- {
- int pivotpos;
- if (low<high)
- {
- pivotpos=partition(a,low,high);
- quicksort(a,low,pivotpos-1);
- quicksort(a,pivotpos+1,high);
- }
- }
- int main()
- {
- int a[]={2,7,9,3,1,4,8,0,6,2};
- int i;
- for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);
- printf("/n");
- quicksort(a,0,ARRLEN(a)-1);
- for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);
- printf("/n");
- return 0;
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。