三郎数据结构算法学习笔记:冒泡排序及其简单优化
发布日期:2021-06-29 20:04:04 浏览次数:2 分类:技术文章

本文共 2792 字,大约阅读时间需要 9 分钟。

三郎数据结构算法学习笔记:冒泡排序及其简单优化

基本思想

冒泡排序(Bubble Sorting)的基本思想是:通过对待排序序列从前向后(从下标较小的元素开始),依次比较相邻元素的值,若发现逆序则交换,使值较大的元素逐渐从前移向后部,就象水底下的气泡一样逐渐向上冒。

优化

因为排序的过程中,各元素不断接近自己的位置,如果一趟比较下来没有进行过交换,就说明序列有序,因此要在 排序过程中设置一个标志 flag 判断元素是否进行过交换。从而减少不必要的比较。

(这里说的优化,可以在冒泡排序写好后进行)

图示

在这里插入图片描述

结果

我们举一个具体的案例来说明冒泡法。我们将五个无序的数:3, 9, -1, 10, -2 使用冒泡排序法将其排成一个从小到大的有序数列

在这里插入图片描述

源代码

/** author:sanlang* time:2020.10.27 19:12* function:bubble sort and simple improvement** */package com.atguigu.sort;import java.text.SimpleDateFormat;import java.util.Arrays;import java.util.Date;public class BubbleSort {	public static void main(String[] args) {		int arr[] = {3, 9, -1, 10, 20};		int temp;		System.out.println("排序前");		System.out.println(Arrays.toString(arr));				//为了容量理解,我们把冒泡排序的演变过程,给大家展示				//测试一下冒泡排序的速度O(n^2), 给80000个数据,测试		//创建要给80000个的随机的数组		int[] arr2 = new int[80000];		for(int i =0; i < 80000;i++) {			arr2[i] = (int)(Math.random() * 8000000); //生成一个[0, 8000000) 数		}				Date data1 = new Date();		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		String date1Str = simpleDateFormat.format(data1);		System.out.println("排序前的时间是=" + date1Str);				//测试冒泡排序		bubbleSort(arr);				Date data2 = new Date();		String date2Str = simpleDateFormat.format(data2);		System.out.println("排序后的时间是=" + date2Str);				System.out.println("排序后");		System.out.println(Arrays.toString(arr));								// 第二趟排序,就是将第二大的数排在倒数第二位				for (int j = 0; j < arr.length - 1 - 1 ; j++) {			// 如果前面的数比后面的数大,则交换			if (arr[j] > arr[j + 1]) {				temp = arr[j];				arr[j] = arr[j + 1];				arr[j + 1] = temp;			}		}				System.out.println("第二趟排序后的数组");		System.out.println(Arrays.toString(arr));						// 第三趟排序,就是将第三大的数排在倒数第三位				for (int j = 0; j < arr.length - 1 - 2; j++) {			// 如果前面的数比后面的数大,则交换			if (arr[j] > arr[j + 1]) {				temp = arr[j];				arr[j] = arr[j + 1];				arr[j + 1] = temp;			}		}		System.out.println("第三趟排序后的数组");		System.out.println(Arrays.toString(arr));				// 第四趟排序,就是将第4大的数排在倒数第4位		for (int j = 0; j < arr.length - 1 - 3; j++) {			// 如果前面的数比后面的数大,则交换			if (arr[j] > arr[j + 1]) {				temp = arr[j];				arr[j] = arr[j + 1];				arr[j + 1] = temp;			}		}		System.out.println("第四趟排序后的数组");		System.out.println(Arrays.toString(arr));			}		// 将前面额冒泡排序算法,封装成一个方法	public static void bubbleSort(int[] arr) {		// 冒泡排序 的时间复杂度 O(n^2), 自己写出		int temp = 0; // 临时变量		boolean flag = false; // 标识变量,表示是否进行过交换		for (int i = 0; i < arr.length - 1; i++) {			for (int j = 0; j < arr.length - 1 - i; j++) {				// 如果前面的数比后面的数大,则交换				if (arr[j] > arr[j + 1]) {					flag = true;					temp = arr[j];					arr[j] = arr[j + 1];					arr[j + 1] = temp;				}			}			System.out.println("第" + (i + 1) + "趟排序后的数组");			System.out.println(Arrays.toString(arr));			if (!flag) { // 在一趟排序中,一次交换都没有发生过				break;			} else {				flag = false; // 重置flag!!!, 进行下次判断			}		}	}}

转载地址:https://blog.csdn.net/m0_51684972/article/details/110069513 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:三郎数据结构算法学习笔记:选择排序
下一篇:前端特效源代码:实现柱状图入门必会 h5+canvs

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月20日 05时55分27秒