用数组实现冒泡排序的精细化分析
【摘要】
视频课堂 https://edu.csdn.net/course/play/8034
/* * 定义一个没有排序的数组,然后使用冒泡排序的算法进行排序,并输出排序后的结果。 * * */public class DemoArray {public static void main(String[]args){int[]ar...
视频课堂 https://edu.csdn.net/course/play/8034
-
/*
-
-
* 定义一个没有排序的数组,然后使用冒泡排序的算法进行排序,并输出排序后的结果。
-
*
-
* */
-
public class DemoArray {
-
public static void main(String[]args){
-
int[]array=new int[]{5,3,6,2,7,4,9,8};
-
//排序前的数组结构
-
System.out.println("排序前的遍历结果是:");
-
for(int n:array)
-
System.out.print(n+"\t");
-
System.out.println("\n--------------------------");
-
//冒泡排序的主要逻辑
-
for(int i=0;i<array.length;i++){
-
for(int j=0;j<array.length-1;j++){
-
int temp=0;
-
if(array[j]<array[j+1]){
-
temp=array[j];
-
array[j]=array[j+1];
-
array[j+1]=temp;
-
}
-
System.out.print("第"+(j+1)+"次排序数组为:");
-
for(int n:array)
-
System.out.print(""+n+"\t");
-
System.out.println(); //测试每次排序后的数组结构.
-
}
-
}
-
//排序后,数组结构发生了改变,元素从大道小排序;
-
System.out.println("排序后的数组结果是:");
-
for(int n:array)
-
System.out.print(n+"\t");
-
System.out.println();
-
}
-
}
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/24551711
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)