java基础内容笔记--冒泡排序

【注意,本人小白一个,以下是个人根据教程的理解,如有疑惑,出错的地方,希望大家能够留言指出来,相互学习进步。】

快速排序笔记

在java基础中,冒泡排序是一种常见的,多用的排序方法,

通过这种方法来进一步的深入数组的实际应用。

冒泡排序是通过两个嵌套的for循环来实现数组内元素的排序,

首先第一个for循环拿出数组第一个元素(下标为0)【标记为i】;

第二个循环是核心的循环,目的是,遍历整个数组,并且在遍历的时候与第一个循环中的数进行比较【用作比较的数标记为j】,

判断出哪个数小,如果j<i,这两个元素交换值,否则跳过,继续下一个数比较。

当第二个循环第一遍的时候,就已经确定了数组下标为0的元素是整个数组的最小值了,

紧接执行第一个循环,拿出下标为1的元素放到第二个循环中比较,方法还是一样。这样也确定了数组下标为1的元素是第二小的数了。当第一个循环完了后,整个数组的元素就会按照从小到大排序。

【下面建议分屏观看代码和控制台结果,有助于理解冒泡的运行原理。】

下面的代码已经有注释,还有但是的思路。

/*
 * 冒泡遍历思路,结合运行结果来分析
 * i是原始比较的数据,即是数组第一个数据,拿来到第二个for循环中用来比较哦的数据,
 * 如果循环中的数据j比i小,这两个数据对换 ,这时候,i就是最小的数值(i的下标是0,也就确定了数组第一个数据是最小的数)
 * 2018年3月17日14:45:47
 */
public class hello_world {
	public static void main(String[] args) {
		int []intArray = {12,11,45,6,8,43,40,57,3,20};//数组
		System.out.println("排序前的数组");
		for(int i=0;i<intArray.length;i++){
			System.out.print(intArray[i]+" ");//遍历数组并输出
		}
		System.out.println(" ");//换行
		int temp;//申明变量
		for(int i=0;i<intArray.length;i++){
			for(int j=i;j<intArray.length;j++){
				System.out.println("intArray[j="+j+"]:"+intArray[j]);
				System.out.println("intArray[i="+i+"]:"+intArray[i]);
				
				if(intArray[j]<intArray[i]){
					System.out.println("判断:intArray[j="+j+"]:"+intArray[j]);
					System.out.println("判断:intArray[i="+i+"]:"+intArray[i]);
					temp=intArray[i];
					System.out.println("temp:"+temp);
					intArray[i]=intArray[j];
					intArray[j]=temp;
				}
				
				System.out.println("遍历:intArray[j="+j+"]:"+intArray[j]);
				System.out.println("*************【第二个for循环】****************");
			}System.out.println("【第一个for循环】===========================【第一个for循环】");
		}
		System.out.println("排序后的数组");
		for (int i = 0; i < intArray.length; i++) {
			System.out.print(intArray[i]+"  ");
		}
	}
}

控制台上的结果。

排序前的数组
12 11 45 6 8 43 40 57 3 20  
intArray[j=0]:12
intArray[i=0]:12
遍历:intArray[j=0]:12
*****************************【第二个for循环】
intArray[j=1]:11
intArray[i=0]:12
判断:intArray[j=1]:11
判断:intArray[i=0]:12
temp:12
遍历:intArray[j=1]:12
*****************************【第二个for循环】
intArray[j=2]:45
intArray[i=0]:11
遍历:intArray[j=2]:45
*****************************【第二个for循环】
intArray[j=3]:6
intArray[i=0]:11
判断:intArray[j=3]:6
判断:intArray[i=0]:11
temp:11
遍历:intArray[j=3]:11
*****************************【第二个for循环】
intArray[j=4]:8
intArray[i=0]:6
遍历:intArray[j=4]:8
*****************************【第二个for循环】
intArray[j=5]:43
intArray[i=0]:6
遍历:intArray[j=5]:43
*****************************【第二个for循环】
intArray[j=6]:40
intArray[i=0]:6
遍历:intArray[j=6]:40
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=0]:6
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:3
intArray[i=0]:6
判断:intArray[j=8]:3
判断:intArray[i=0]:6
temp:6
遍历:intArray[j=8]:6
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=0]:3
遍历:intArray[j=9]:20
*****************************【第二个for循环】
第1次循环后的数组:
3  12  45  11  8  43  40  57  6  20  
【第一个for循环】===========================
intArray[j=1]:12
intArray[i=1]:12
遍历:intArray[j=1]:12
*****************************【第二个for循环】
intArray[j=2]:45
intArray[i=1]:12
遍历:intArray[j=2]:45
*****************************【第二个for循环】
intArray[j=3]:11
intArray[i=1]:12
判断:intArray[j=3]:11
判断:intArray[i=1]:12
temp:12
遍历:intArray[j=3]:12
*****************************【第二个for循环】
intArray[j=4]:8
intArray[i=1]:11
判断:intArray[j=4]:8
判断:intArray[i=1]:11
temp:11
遍历:intArray[j=4]:11
*****************************【第二个for循环】
intArray[j=5]:43
intArray[i=1]:8
遍历:intArray[j=5]:43
*****************************【第二个for循环】
intArray[j=6]:40
intArray[i=1]:8
遍历:intArray[j=6]:40
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=1]:8
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:6
intArray[i=1]:8
判断:intArray[j=8]:6
判断:intArray[i=1]:8
temp:8
遍历:intArray[j=8]:8
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=1]:6
遍历:intArray[j=9]:20
*****************************【第二个for循环】
第2次循环后的数组:
3  6  45  12  11  43  40  57  8  20  
【第一个for循环】===========================
intArray[j=2]:45
intArray[i=2]:45
遍历:intArray[j=2]:45
*****************************【第二个for循环】
intArray[j=3]:12
intArray[i=2]:45
判断:intArray[j=3]:12
判断:intArray[i=2]:45
temp:45
遍历:intArray[j=3]:45
*****************************【第二个for循环】
intArray[j=4]:11
intArray[i=2]:12
判断:intArray[j=4]:11
判断:intArray[i=2]:12
temp:12
遍历:intArray[j=4]:12
*****************************【第二个for循环】
intArray[j=5]:43
intArray[i=2]:11
遍历:intArray[j=5]:43
*****************************【第二个for循环】
intArray[j=6]:40
intArray[i=2]:11
遍历:intArray[j=6]:40
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=2]:11
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:8
intArray[i=2]:11
判断:intArray[j=8]:8
判断:intArray[i=2]:11
temp:11
遍历:intArray[j=8]:11
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=2]:8
遍历:intArray[j=9]:20
*****************************【第二个for循环】
第3次循环后的数组:
3  6  8  45  12  43  40  57  11  20  
【第一个for循环】===========================
intArray[j=3]:45
intArray[i=3]:45
遍历:intArray[j=3]:45
*****************************【第二个for循环】
intArray[j=4]:12
intArray[i=3]:45
判断:intArray[j=4]:12
判断:intArray[i=3]:45
temp:45
遍历:intArray[j=4]:45
*****************************【第二个for循环】
intArray[j=5]:43
intArray[i=3]:12
遍历:intArray[j=5]:43
*****************************【第二个for循环】
intArray[j=6]:40
intArray[i=3]:12
遍历:intArray[j=6]:40
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=3]:12
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:11
intArray[i=3]:12
判断:intArray[j=8]:11
判断:intArray[i=3]:12
temp:12
遍历:intArray[j=8]:12
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=3]:11
遍历:intArray[j=9]:20
*****************************【第二个for循环】
第4次循环后的数组:
3  6  8  11  45  43  40  57  12  20  
【第一个for循环】===========================
intArray[j=4]:45
intArray[i=4]:45
遍历:intArray[j=4]:45
*****************************【第二个for循环】
intArray[j=5]:43
intArray[i=4]:45
判断:intArray[j=5]:43
判断:intArray[i=4]:45
temp:45
遍历:intArray[j=5]:45
*****************************【第二个for循环】
intArray[j=6]:40
intArray[i=4]:43
判断:intArray[j=6]:40
判断:intArray[i=4]:43
temp:43
遍历:intArray[j=6]:43
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=4]:40
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:12
intArray[i=4]:40
判断:intArray[j=8]:12
判断:intArray[i=4]:40
temp:40
遍历:intArray[j=8]:40
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=4]:12
遍历:intArray[j=9]:20
*****************************【第二个for循环】
第5次循环后的数组:
3  6  8  11  12  45  43  57  40  20  
【第一个for循环】===========================
intArray[j=5]:45
intArray[i=5]:45
遍历:intArray[j=5]:45
*****************************【第二个for循环】
intArray[j=6]:43
intArray[i=5]:45
判断:intArray[j=6]:43
判断:intArray[i=5]:45
temp:45
遍历:intArray[j=6]:45
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=5]:43
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:40
intArray[i=5]:43
判断:intArray[j=8]:40
判断:intArray[i=5]:43
temp:43
遍历:intArray[j=8]:43
*****************************【第二个for循环】
intArray[j=9]:20
intArray[i=5]:40
判断:intArray[j=9]:20
判断:intArray[i=5]:40
temp:40
遍历:intArray[j=9]:40
*****************************【第二个for循环】
第6次循环后的数组:
3  6  8  11  12  20  45  57  43  40  
【第一个for循环】===========================
intArray[j=6]:45
intArray[i=6]:45
遍历:intArray[j=6]:45
*****************************【第二个for循环】
intArray[j=7]:57
intArray[i=6]:45
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:43
intArray[i=6]:45
判断:intArray[j=8]:43
判断:intArray[i=6]:45
temp:45
遍历:intArray[j=8]:45
*****************************【第二个for循环】
intArray[j=9]:40
intArray[i=6]:43
判断:intArray[j=9]:40
判断:intArray[i=6]:43
temp:43
遍历:intArray[j=9]:43
*****************************【第二个for循环】
第7次循环后的数组:
3  6  8  11  12  20  40  57  45  43  
【第一个for循环】===========================
intArray[j=7]:57
intArray[i=7]:57
遍历:intArray[j=7]:57
*****************************【第二个for循环】
intArray[j=8]:45
intArray[i=7]:57
判断:intArray[j=8]:45
判断:intArray[i=7]:57
temp:57
遍历:intArray[j=8]:57
*****************************【第二个for循环】
intArray[j=9]:43
intArray[i=7]:45
判断:intArray[j=9]:43
判断:intArray[i=7]:45
temp:45
遍历:intArray[j=9]:45
*****************************【第二个for循环】
第8次循环后的数组:
3  6  8  11  12  20  40  43  57  45  
【第一个for循环】===========================
intArray[j=8]:57
intArray[i=8]:57
遍历:intArray[j=8]:57
*****************************【第二个for循环】
intArray[j=9]:45
intArray[i=8]:57
判断:intArray[j=9]:45
判断:intArray[i=8]:57
temp:57
遍历:intArray[j=9]:57
*****************************【第二个for循环】
第9次循环后的数组:
3  6  8  11  12  20  40  43  45  57  
【第一个for循环】===========================
intArray[j=9]:57
intArray[i=9]:57
遍历:intArray[j=9]:57
*****************************【第二个for循环】
第10次循环后的数组:
3  6  8  11  12  20  40  43  45  57  
【第一个for循环】===========================
排序后的数组
3  6  8  11  12  20  40  43  45  57  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值