算法入门-快速排序-基本快速排序方法
来源:互联网 发布:乐高机器人怎么编程 编辑:程序博客网 时间:2024/06/06 21:41
#include <cstdlib>#include <iostream>using namespace std;int a[10]={49,38, 65, 97, 76, 13, 27 };void exchange(int p,int q){ int temp; temp = a[p]; a[p] = a[q]; a[q] = temp;}void quicksort(int begin, int end){ int p,r; int key = a[begin]; p = begin; r = end; if(p>r) return; while(p<r) { while((p<r)&&a[r]>=key) r--; swap(a[p],a[r]); while((p<r)&&a[p]<key) p++; swap(a[r],a[p]); } quicksort(begin,r-1); quicksort(r+1,end);}int main(int argc, char *argv[]){ int length = 7; quicksort(0,6); int i=0; for(i=0;i<7;i++) printf("%d ",a[i]); system("PAUSE"); return EXIT_SUCCESS;}