您的位置:首页 > 其它

默写简单的排序算法

2016-03-15 09:28 357 查看
快速排序:
public void sort(int arr[],int low,int high)
 {
 int l=low;
 int h=high;
 int povit=arr[low];

 while(l<h)
 {
 while(l<h&&arr[h]>=povit)
 h--;
 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 l++;
 }

 while(l<h&&arr[l]<=povit)
 l++;

 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 h--;
 }
 }

 if(l>low)sort(arr,low,l-1);
 if(h<high)sort(arr,l+1,high);
 }


冒泡排序:

public void sort(int[] a)

{

int temp = 0;

for (int i = a.length - 1; i > 0; --i)

{

for (int j = 0; j < i; ++j)

{

if (a[j + 1] < a[j])

{

temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: