您的位置:首页 > 其它

算法题目-冒泡排序

2017-06-12 20:52 183 查看
冒泡排序,是一种经典的排序算法,

冒泡排序的最差、平均时间复杂度都为O(n^2),

空间复杂度为O(1),

它是一种稳定的排序算法。

package Website;

public class sort {
public static void main(String[] args) {
int[] arr={1,5,3,2,7,8,9,4,6};
System.out.print("排序前:");
sort.output(arr);
sort.BubbleSort(arr);
System.out.println();
System.out.print("排序后:");
sort.output(arr);
}
public static void BubbleSort(int[] a){
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
public static void output(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}


输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: