排序算法——冒泡排序(Bubble Sort)


算法简介(Introduction)
Bubble sort is to compare adjacent elements of the list and exchange them as long as they are out of order. By repeatly compare and exchange, the largest element “bubbling up” go to last position in the list. In the second pass, the second largest element bubbles up to last second position. After n-1 passes, the list is sorted. In the ith pass, the state of list represented as follow:

(picture is from “Introduction to The Design and analysis of Algorithms” page 100)

示例(Example)

In the first pass, 100 bubbles up to last position.

伪代码(Pseudocode)

function BubbleSort(A[0..n-1])for i ⟵ 0 to n-2 dofor j ⟵ 0 to n-2-i doif A[j] > A[j+1] thenswap A[j] and A[j+1]

基本属性(property)
Input: an array A[0..n-1] of n orderable items.

Output: an array A[0..n-1] sorted in non-descending order.

In-place: YES. It only needs a constant amount O(1) of additional memory apace.

Stable: YES. Does not change the relative order of elements with equal keys.

时间复杂度(Time Complexity)
The input size is n.
the basic operation is key comparison A[j] > A[j+1].
The amount of times the basic operation executed is Cn.

适用情形(Suitable Situation)
Bubble sort is one of brute force sorting algorithms. It has simple idea that is to compare pair of adjacent elements and to swap. But it’s very slow and impractical for most problems. Even compared to insertion sort. It might be helpful when the input is sorted while having some occasional out-of-order elements.

Java Code

public class Sort{//Bubble sort methodpublic static int[] bubbleSort(int[] A){int i,j,tmp;for(i=0;i<A.length-1;i++)for(j=0;j<A.length-1-i;j++)if(A[j] > A[j+1]){tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;}return A;}//Testpublic static void main(String[] args){int[] A={45,23,100,28,89,59,72};int[] sortedA=Sort.bubbleSort(A);for(int i=0;i<sortedA.length;i++)System.out.print(A[i]+" ");}
}

运行结果(Result)

23 28 45 59 72 89 100 

写在最后的话(PS)
Welcome any doubt. my email address is shuaiw6@student.unimelb.edu.au

排序算法——冒泡排序(Bubble Sort)相关推荐

  1. 经典排序算法 - 冒泡排序Bubble sort

    经典排序算法 - 冒泡排序Bubble sort 其原理是比较接近的数字22,按照从小到交换大或降序排列, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头開始进行两两比較交换,直到倒 ...

  2. 基础排序算法 – 冒泡排序Bubble sort

    原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束. 从小到大排序: 原 ...

  3. 排序 时间倒序_经典排序算法之冒泡排序(Bubble Sort)

    冒泡排序 ( Bubble Sort ) 冒泡排序,正如它的名字一样,未排序数组中的最大(小)值会依次往上浮.冒泡排序主要有两个基本步骤:相邻元素之间的比较 和 交换位置. 步骤分析: 令待排序序列为 ...

  4. 【算法】Bubble Sort(泡式排序)的编程实现思路及其复杂度分析==>冒泡排序

    冒泡排序 Bubble Sort的复杂度分析 什么是复杂度? 简单的来说当我们需要衡量算法的优异程度的时候就需要用到它... 对一个算法来说,我们一般用时间和空间这两个维度来衡量它.也就是算法的执行时 ...

  5. php编写冒泡排序算法_PHP排序算法之冒泡排序(Bubble Sort)实现方法详解

    本文实例讲述了PHP排序算法之冒泡排序(Bubble Sort)实现方法.分享给大家供大家参考,具体如下: 基本思想: 冒泡排序是一种交换排序,它的基本思想是:两两比较相邻记录的关键字,如果反序则交换 ...

  6. 7.使用php描述冒泡排序,PHP 数据结构 算法描述 冒泡排序 bubble sort

    PHP 数据结构 算法描述 冒泡排序 bubble sort 复制代码 代码如下: /** * 冒泡排序 bubble sort * * 原理:多次循环进行比较,每次比较时将最大数移动到最上面.每次循 ...

  7. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2 ...

  8. 排序算法---冒泡排序(java版)

    冒泡排序 原理 冒泡排序(Bubble Sort)是一种简单的排序算法,它通过依次比较两个相邻的的元素,看两个元素是否满足大小关系要求,如果不满足则交换两个元素.每一次冒泡会让至少一个元素移动到它应该 ...

  9. 十大经典排序算法-冒泡排序算法详解

    十大经典排序算法 十大经典排序算法-冒泡排序算法详解 十大经典排序算法-选择排序算法详解 十大经典排序算法-插入排序算法详解 十大经典排序算法-希尔排序算法详解 十大经典排序算法-快速排序算法详解 十 ...

最新文章

  1. 结果方程模型(SEM)的理论和基本实现过程
  2. 省时省事省力 巧用阿里ECS D1构建大数据处理平台
  3. 【LeetCode】0046.全排列 (递归详解)
  4. Android HelloWorld 例子
  5. 用jedis访问Redis进行对象存取示例
  6. C++内部类访问外部类
  7. apache重写规则转Nginx
  8. V9任何页面GET调用内容分页的说明
  9. uboot mkimage使用详解
  10. 云湖共生-释放企业数据价值
  11. Linux服务之httpd基本配置详解
  12. IE 6 特有的条件注释详情
  13. 分布式事务模型--Saga
  14. 【机器学习】监督学习--(回归)岭回归
  15. uitableView group模式下的间距问题
  16. oracle 应收 系统选项,Oracle财务系统应收账款模块操作手册
  17. 计算机内存的存储单位换算,电脑内存换算(电脑内存单位及换算)
  18. yy自动语音接待机器人_YY自动欢迎老板,全自动欢迎,来人自动欢迎广播(文字欢迎)...
  19. windows替换鼠标指针
  20. 0717Python总结-return返回值,全局及局部变量,函数名的使用,函数的嵌套,nonlocal修改局部变量,及locals和globals

热门文章

  1. 服务器摆放需要预留U位么_办公沙发摆放有何讲究?
  2. Linux网络编程(四)
  3. 前端面试题集锦——JavaScript
  4. 理财U24 认股权证、可赎债、可转债、永续债、优先股 教材解读
  5. 一维数组的定义以及使用
  6. Unity调用大华相机SDK采集图像及基本功能设定
  7. 计算机网络应用层和传输层及网络层协议有哪些?
  8. 微服务架构-ruoyi
  9. 一键搞定身份证复印 多功能应用全面满足工作组需求
  10. Matlab中的ismember和contains傻傻分不清