排序

来源:互联网 发布:linux 深度压缩命令 编辑:程序博客网 时间:2024/05/01 16:02

package sort;

 

public class SortSample {

public static int [] budding(int []obj){

int len=obj.length;

int temp;

int i,j;

for(i=0;i<len-1;i++){

for(j=0;j<len-i-1;j++){

if(obj[j]>obj[j+1]){

temp=obj[j];

obj[j]=obj[j+1];

obj[j+1]=temp;

}

}

}

return obj;

}

public static int[]choose(int[]obj){

int len=obj.length;

int i,j;

int temp;

for(i=0;i<len-1;i++){

for(j=i+1;j<len;j++)

if(obj[i]>obj[j]){

temp=obj[i];

obj[i]=obj[j];

obj[j]=temp;

}

}

return obj;

}

public static int[]quick_sort(int []obj,int low ,int high){

int len=obj.length;

int i,j,t;

if(low<high){

i=low;

j=high;

t=obj[low];

while(i<j){

while(i<j&&obj[j]>t)j--;

if(i<j){

obj[i]=obj[j];i++;

}

while(i<j&&obj[i]<=t)i++;

if(i<j){

obj[j]=obj[i];j--;

}

}

obj[i]=t;

quick_sort(obj,low,i-1);

quick_sort(obj,i+1,high);

}

return obj;

}

public static void main(String[]args){

 

int[]a=new int[]{3,7,6,2,9,1,12,3,8,1,0,5};

int []b;

System.out.println("......冒泡排序结果。。。。");

b=budding(a);

for(int i=0;i<b.length;i++)

System.out.print(b[i]);

System.out.println();

System.out.println("......选择排序结果。。。。");

b=choose(a);

for(int i=0;i<b.length;i++)

System.out.print(b[i]);

System.out.println();

System.out.println("......快速排序结果。。。。");

b=quick_sort(a,0,a.length-1);

for(int i=0;i<b.length;i++)

System.out.print(b[i]);

System.out.println();

}

 

}

原创粉丝点击