冒泡排序算法改进版 算法
程序员文章站
2022-05-21 17:14:13
...
public class BubbleRevised {
/**@author liuwei
* @param args
*/
public static void bubbleSort(int[] list){
boolean needNextPass = true;
for(int k=1;k < list.length && needNextPass; k++){
needNextPass = false;
for(int i=0;i < list.length - k; i++){
if(list[i] > list[i+1]){
int temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
needNextPass = true;
}
}
}
}
public static void main(String[] args) {
int[] list = {2,3,2,5,6,1,-2,3,14,12};
bubbleSort(list);
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
}
}
/**@author liuwei
* @param args
*/
public static void bubbleSort(int[] list){
boolean needNextPass = true;
for(int k=1;k < list.length && needNextPass; k++){
needNextPass = false;
for(int i=0;i < list.length - k; i++){
if(list[i] > list[i+1]){
int temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
needNextPass = true;
}
}
}
}
public static void main(String[] args) {
int[] list = {2,3,2,5,6,1,-2,3,14,12};
bubbleSort(list);
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
}
}
上一篇: java多线程总结(转)
下一篇: 动态语言&动态类型语言&静态类型语言