java语言,eclipse开发环境
//快速排序
public static void sort(int[] a,int left,int right){
int i=left;
int j=right;
int temp;
if(left<right){
temp=a[left];
while(i!=j){
while(j>i&&a[j]>=temp){
j--;
}
if(i<j){
a[i]=a[j];
i++;
}
while(i<j&&a[i]<=temp){
i++;
}
if(i<j){
a[j]=a[i];
j--;
}
}
}
else{
return;
}
a[i]=temp;
sort(a,left,i-1);
sort(a,i+1,right);
}
//冒泡排序
public static void sort(int[] a){
boolean flag=true;
for(int i=0;i<a.length-1;i++){
for(int j=1;j<a.length-i;j++){
if(a[j-1]>a[j]){
flag=false;
int temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
if(flag==true){
// System.out.println("It's odered");
break;
}
}
}
//插入排序
public static void sort(int[] a){
int temp, j;
for(int i=1;i<a.length;i++){
temp=a[i];
j=i-1;
while(j>=0&&temp<a[j]){
//while(temp<a[j]){ //error
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
//二分插入排序
public static void sort(int[] a){
int low,mid,high,temp;
for(int i=1;i<a.length;i++){
low=0;
high=i-1;
//mid=(low+high)/2;
temp=a[i];
while(low<=high){
mid=(low+high)/2;
if(temp>a[mid]){
low=mid+1;
}
else{
high=mid-1;
}
}
for(int j=i-1;j>=high+1;j--){
a[j+1]=a[j];
}
a[high+1]=temp;
}
}
//测试程序
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={8,9,5,7,2,4,1,6,3,0};
//int[] a={1,2,3,4,5,6,7,8,9};
System.out.println("before sort:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
sort(a);
//sort(a,0,a.length-1);//quick_sort
System.out.println("after sort:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}