http://www.algolist.net/Algorithms/

https://docs.oracle.com/javase/tutorial/collections/algorithms/

https://en.wikipedia.org/wiki/Sorting_algorithm

冒泡排序(Bubble sort)

https://en.wikipedia.org/wiki/Bubble_sort

loop1:

4,6,1,3,7 -> 4,6,1,3,7

4,6,1,3,7 -> 4,1,6,3,7

4,1,6,3,7 -> 4,1,3,6,

4,1,3,6,7 -> 4,1,3,6,7

loop2:

4,1,3,6,7 -> 1,4,3,6,7

1,4,3,6,7 -> 1,3,4,6,7

1,3,4,6,7 -> 1,3,4,6,7

1,3,4,6,7 -> 1,3,4,6,7

loop3:

1,3,4,6,7 -> 1,3,4,6,7

1,3,4,6,7 -> 1,3,4,6,7

1,3,4,6,7 -> 1,3,4,6,7

                                                            1,3,4,6,7 -> 1,3,4,6,7

当第三次循环,没有发生swap 说明已排序完成 ,应 break

冒泡排序特点:

1)比较相邻的两个数

2)只能通过判断没有交换来提前结束

最好的情况:

loop1:

1,2,3 -> 1,2,3

1,2,3 -> 1,2,3

最坏的情况:

loop1:

3,2,1 -> 2,3,1

2,3,1 -> 2,1,3

loop2:

2,1,3 -> 1,2,3

1,2,3 -> 1,2,3

loop3:

1,2,3 -> 1,2,3

1,2,3 -> 1,2,3

package sorting;

import java.util.Arrays;

import org.junit.Test;

public class BubbleSorting {

    int[] items = { 4, 6, 1, 3, 7 };
int step = 0;
// ① 相邻
// ② 差一步
// ③ n个数可产生 n-1 对 @Test
public void sort() {
for (;;) {
boolean swapped = false;
for (int i = 0; i < items.length - 1; i++) {
step++;
if (items[i] > items[i + 1]) {
swap(i, i + 1);
swapped = true;
}
}
if (!swapped)
break;
} System.out.println(step + ":" + Arrays.toString(items));
} public void swap(int i, int j) {
int backup = items[i];
items[i] = items[j];
items[j] = backup;
} }

优化1(砍掉最后一个)

Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

loop1:

4,6,1,3,7 -> 4,6,1,3,7

4,6,1,3,7 -> 4,1,6,3,7

4,1,6,3,7 -> 4,1,3,6,7

4,1,3,6,7 -> 4,1,3,6,7

loop2:

4,1,3,6 -> 1,4,3,6

1,4,3,6 -> 1,3,4,6

1,3,4,6 -> 1,3,4,6

loop3:

1,3,4 -> 1,3,4

1,3,4 -> 1,3,4

无swap 结束

package sorting;

import java.util.Arrays;

import org.junit.Test;

public class BubbleSorting {

    int[] items = { 4, 6, 1, 3, 7 };
int step = 0;
// ① 相邻
// ② 差一步
// ③ n个数可产生 n-1 对
// ④ 把最大(小)数移到末尾,n = n -1 来缩小循环次数 @Test
public void sort() {
int l = items.length;
for (;;) {
boolean swapped = false; for (int i = 1; i < l; i++) {
step++;
if (items[i - 1] > items[i]) {
swap(i - 1, i);
swapped = true;
}
}
l = l - 1;
if (!swapped)
break;
} System.out.println(step + ":" + Arrays.toString(items));
} public void swap(int i, int j) {
int backup = items[i];
items[i] = items[j];
items[j] = backup;
} }

优化2(砍掉最后一段)

loop1:

4,6,1,3,7 -> 4,6,1,3,7

4,6,1,3,7 -> 4,1,6,3,7

4,1,6,3,7 -> 4,1,3,6,7

4,1,3,6,7 -> 4,1,3,6,7

loop2:

4,1,3 -> 1,4,3

1,4,3 -> 1,3,4

loop3:

1,3 -> 1,3

package sorting;

import java.util.Arrays;

import org.junit.Test;

public class BubbleSorting {

    int[] items = { 4,6,1,3,7 };
int step = 0;
// ① 相邻
// ② 差一步
// ③ n个数可产生 n-1 对
// ④ 找到最后一次交换下标,只保留前面部分
// ⑤ 当 最后一次交换下标 == 0 时,说明没有交换,则终止循环 @Test
public void sort() {
int l = items.length;
for (;;) {
int lastSwapIndex = 0;
for (int i = 1; i < l; i++) {
step++;
if (items[i - 1] > items[i]) {
swap(i - 1, i);
lastSwapIndex = i;
}
}
l = lastSwapIndex;
if (l < 1)
break;
} System.out.println(step + ":" + Arrays.toString(items));
} public void swap(int i, int j) {
int backup = items[i];
items[i] = items[j];
items[j] = backup;
} }

排序算法 (sorting algorithm)之 冒泡排序(bubble sort)的更多相关文章

  1. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  2. 排序的本质是什么 冒泡排序 bubble sort

    import random def getRandomNums(min=4, max=20): n = random.randint(min, max) arr = [random.randint(1 ...

  3. java排序算法之冒泡排序(Bubble Sort)

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

  4. 排序算法--冒泡排序(Bubble Sort)_C#程序实现

    排序算法--冒泡排序(Bubble Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来非常困 ...

  5. 【排序算法】冒泡排序(Bubble Sort)

    0. 说明 参考 维基百科中的冒泡排序 冒泡排序 (Bubble Sort) 是与插入排序拥有相等的执行时间,但是两种算法在需要的交换次数却很大地不同. 在最坏的情况,冒泡排序需要 O(n2)  次交 ...

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

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  7. 排序算法(sorting)

    学习到的排序算法的总结,包括对COMP20003中排序部分进行总结,部分图片来自COMP20003 有部分内容来自http://www.cnblogs.com/eniac12/p/5329396.ht ...

  8. Python之排序算法:快速排序与冒泡排序

    Python之排序算法:快速排序与冒泡排序 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7828610.html 入坑(简称IT)这一行也有些年头了,但自老师 ...

  9. 冒泡排序(Bubble Sort),比较次数优化改进

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  10. 算法之经典排序-冒泡排序(bubble sort)

    冒泡排序 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 这个算法的名字由来是因为越大的元 ...

随机推荐

  1. python待学习内容

    1.Python中不尽如人意的断言Assertion https://www.cnblogs.com/cicaday/p/python-assert.html 2.Python中的反转字符串问题 ht ...

  2. 基于WebGL架构的3D可视化平台—新风系统演示

    新风系统是根据在密闭的室内一侧用专用设备向室内送新风,再从另一侧由专用设备向室外排出,在室内会形成“新风流动场”,从而满足室内新风换气的需要.实施方案是:采用高风压.大流量风机.依靠机械强力由一侧向室 ...

  3. JS实现将数字金额转换为大写人民币汉字

    function convertCurrency(money) { //汉字的数字 var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', ...

  4. weui复选框无法传值

    //原来的反了 function changeState (ele) { if(ele.checked){ ele.setAttribute('checked','checked') console. ...

  5. Java学习笔记(二十四):单例设计模式singleton

    为什么要使用singleton设计模式? 假设设计了一个操作数组的工具类(Tools),里面有一个锤子的方法(Hammer),如果不使用singleton设计模式,每次想调用Hammer方法都需要ne ...

  6. C#移除URL上指定的参数

    /// <summary>        /// 移除URL上指定的参数,不区分参数大小写        /// </summary>        public static ...

  7. laravel 模型操作

    1. 简介 2. 创建模型 //模型文件默认创建在app目录下,也可以指定创建在某个文件夹下,如Model/Goods 1. php artisan make:model Goods 2. 这种方式会 ...

  8. 19. Remove Nth Node From End of List (JAVA)

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  9. STM32的命名规范

    STM32F407VET6 STM32F407代表的是芯片的型号后面的字符表示芯片的信息 V这一项代表引脚数,其中T代表36脚,C代表48脚,R代表64脚,V代表100脚,Z代表144脚,I代表176 ...

  10. 解析ReentrantLock实现原理

    在Java中通常实现锁有两种方式,一种是synchronized关键字,另一种是Lock(Lock的实现主要有ReentrantLock.ReadLock和WriteLock).synchronize ...