1.快速排序
#include <iostream>
using namespace std;
const int MAXN=10010;
int a[MAXN];
int Partition(int a[],int low,int high)
{
int temp=a[low];
while(low<high)//新的感悟:循环中必须先从high开始,因为最开始只有low的位置可以被覆盖
{
while(low<high&&a[high]>=temp)
high--;
a[low]=a[high];//执行此句后必须转移到low
while(low<high&&a[low]<=temp)
low++;
a[high]=a[low];//执行此句后必须转移
}
a[low]=temp;
return low;
}
void QuickSort(int a[],int low,int high)
{
if(low<high){
int m=Partition(a,low,high);
QuickSort(a,m+1,high);
QuickSort(a,low,m-1);
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
QuickSort(a,1,n);
for(int i=1;i<=n;i++){
cout<<a[i]<<endl;
}
return 0;
}
2.归并排序
3.二分搜索(非递归)
3.二分搜索(递归)
#