- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
ch7_11.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //**********************
- //** ch7_11.cpp **
- //**********************
- #include <iostream.h>
- void qsort(int[],int,int);
- void main()
- {
- int array[]={55,2,6,4,11,12,9,73,26,37};
- int len=sizeof(array)/sizeof(int);
- for(int i=0; i<len; i++) //原始顺序输出
- cout <<array[i] <<",";
- cout <<endl <<endl;
- qsort(array,0,len-1); //调用排序函数
- for(int i=0; i<len; i++) //排序结果输出
- cout <<array[i]<<",";
- cout <<endl;
- }
- void qsort(int a[],int left,int right) //快速排序法
- {
- int pivot=a[right],l=left,r=right,temp;
- while(l<r){
- temp=a[l], a[l]=a[r], a[r]=temp; //交换
- while(l<r && a[r]>pivot) --r;
- while(l<r && a[l]<=pivot) ++l;
- }
- temp=a[left], a[left]=a[r], a[r]=temp; //使得a[r]成为分界元
- if(left<r-1) qsort(a,left,r-1);
- if(r+1<right) qsort(a,r+1,right);
- }