快速排序
#include<iostream>
#define MAXSIZE 10005
using namespace std;
int a[MAXSIZE];
int paritition(int a[],int low,int high){
int pivot=a[low];
while(low<high){
while(low<high&&a[high]>=pivot) --high;
a[low]=a[high];
while(low<high&&a[low]<=pivot) ++low;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void Quicksort(int a[],int low,int high){
if(low<high){
int pivot=paritition(a,low,high);
Quicksort(a,low,pivot-1);
Quicksort(a,pivot+1,high);
}
}
int main(){
int N,i;
cin>>N;
for(i=0;i<N;i++)
cin>>a[i];
Quicksort(a,0,N-1);
cout<<a[i/2]<<endl;
}
归并排序
#include<iostream>
#define MAXSIZE 100
using namespace std;
void Merge(int a[],int s,int m,int t){
int temp[MAXSIZE];
int i=s;
int j=m+1;
int k=i;
while(i<=m&&j<=t)
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
while(i<=m)
temp[k++]=a[i++];
while(j<=t)
temp[k++]=a[j++];
for(i=s;i<=t;i++)
a[i]=temp[i];
}
void Mergesort(int a[],int s,int t){
if(s==t) return;
int m=(t+s)/2;
Mergesort(a,s,m);
Mergesort(a,m+1,t);
Merge(a,s,m,t);
}
int main(){
int N,a[MAXSIZE];
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
Mergesort(a,0,N-1);
for(int i=0;i<N;i++)
cout<<a[i]<<endl;
}