冒泡排序优化算法(复杂度是n2)

来源:互联网 发布:驴友用的gps软件 编辑:程序博客网 时间:2024/06/06 08:46
package sort;


public class BubbleSort {


public static void main(String[] args) {
int[] a={1,2,3,4,5,6,7,8,9,10};
for (int k=0;k<a.length;k++){
System.out.print(a[k]+" ");
}
sortMethod(a);
}



public static void sortMethod(int[]s){
int temp;
boolean flag=true;
for(int i=0;i<s.length-1&&flag;i++){
flag=false;
for(int j=s.length-1;j>i;j--)
{
if (s[j]<s[j-1])
{
temp=s[j];
s[j]=s[j-1];
s[j-1]=temp;
flag=true;
}
}
}
for (int k=0;k<s.length;k++){
System.out.print(s[k]+" ");
}

}
}
0 0
原创粉丝点击