冒泡排序算法的java实现:
public class bubble_sort {
public static void main(String[] args) {
// 冒泡排序的java实现
ArrayList<Integer> a = new ArrayList<Integer>();
//从控制台获取要排序的数,“#”键结束
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
if(sc.equals("#")){
break;
}
a.add(sc.nextInt());
}
for(int i = 0;i<a.size();i++){
for(int j = a.size()-1;j>i;j--){
if(a.get(j)<a.get(j-1)){
int temp = a.get(j);
a.set(j, a.get(j-1));
a.set(j-1, temp);
}
}
}
System.out.println("排序后:");
for(Integer i:a){
System.out.print(i+" ");
}
}
}
相比递归的思想,还是简单的一批,它的时间复杂度是