排序算法

//快速排序

private static void Quick(int[] a,int low,int high) {

if(low<high)
{
int position=Partition(a,low, high);
Quick(a,low,position-1);
Quick(a,position+1,high);
}
}


private static int Partition(int[] a, int low, int high) {
int i=low-1;
int m=a[high];
for(int j=low;j<=high-1;j++){
if(a[j]<=m){
i++;
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
int temp=a[i+1];
a[i+1]=a[high];
a[high]=temp;
return i+1;

}

=========================================================================

//堆排序的建堆过程

private static void BulidHeap(int[] a, int len) {
for(int i=len/2;i>=1;i--){
AdjustDown(a,i,len);
}
}


private static void AdjustDown(int[] a, int i, int len) {
if(i*2<=len){
if(i*2+1<=len){
int max=a[i*2-1]>a[i*2]?a[i*2-1]:a[i*2];
if(a[i-1]<max)
{
if(max==a[i*2-1])
{
int t=a[i-1];
a[i-1]=a[i*2-1];
a[i*2-1]=t;
AdjustDown( a,  i*2, len);
}
else{
int t=a[i-1];
a[i-1]=a[i*2];
a[i*2]=t;
AdjustDown( a,  i*2+1, len);
}


}


}
else{
if(a[i-1]<a[i*2-1]){
int t=a[i-1];
a[i-1]=a[i*2-1];
a[i*2-1]=t;
AdjustDown( a,  i*2, len);
}
}
}
}

====================================================

二路归并

private static void MergeSort(int[] a, int low, int high) {
if(low<high){
int mid=(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Merge(a,low ,mid,high);
}
}


private static void Merge(int[] a, int low, int mid, int high) {
int[] arr=new int[high-low+1];
for(int i=low,j=0;i<=high;i++){
arr[j]=a[i];
j++;
}
int i=0;
int j=mid-low+1;
int z=low;
while(i<=mid-low && j<=high-low){
if(arr[i]<arr[j]){
a[z]=arr[i];
i++;
}
else{
a[z]=arr[j];
j++;
}
z++;
}

if(i>mid-low){
while(j<=high-low){
a[z]=arr[j];
j++;
z++;
}
}
else{
while(i<=mid-low){
a[z]=arr[i];
i++;
z++;
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值