Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows:

1.Define an array,The length is N.

2.Compares each pair of adjacent items and swaps them if they are in the wrong order.

 such as:

   if (a[j - 1] > a[j]) {//The number in front is larger than that in the back.                 //swap a[j-1]和a[j]    int temp;       //Initial value of definition temp.    temp = a[j - 1];    a[j - 1] = a[j];    a[j] = temp;

3.N=N-1,If N is not 0, repeat the previous two steps, otherwise the sorting is completed.

Implementation:

utility class:

public class BubbleSort {  public static void mian(String args[])      {       //ToDo      }
    public static <T extends Comparable<? super T>>void bubbleSort (T[]arr){        int n = arr.length;        int boundary;//Trailing edge traversal of records.        do {            boundary = 0;            for (int i = 1; i < n; i++)                if (arr[i - 1].compareTo(arr[i]) > 0) {                    swap(Arrays.asList(arr), i - 1, i);                    boundary = i;//The boundary value is reset after each comparison, and if this line is not executed during the comparison, the sorting is done.                }            n = boundary;        } while (boundary > 0);    }}

implementation class:

class Numbers implements Comparable<Numbers>{    private int number;    public int getNumber(){        return number;    }    public void setNumber(){        this.number=number;    }    public Numbers(int number){        super();        this.number=number;    }    @Override  public String toString(){        return "[number="+number+"]";    }    @Override    public int compareTo(Numbers o) {        if(number==o.number)            return 0;        else if(number>o.number)            return 1;        else            return -1;

    }}

Over.

Thanks.2018-09-24.

 



Bubble sort of sorting algorithm的更多相关文章

  1. POJ3761 Bubble Sort (组合数学,构造)

    题面 Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be ...

  2. 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)

    http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...

  3. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  4. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  5. HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)

    Sample Input 3 9 8 7 5 1 9 2 6 4 3 1 2 3 4 5 6 7 8 9 9 8 7 5 1 9 2 6 4 3 1 2 5 4 3 6 7 8 9 9 8 7 5 1 ...

  6. 「SP25784」BUBBLESORT - Bubble Sort 解题报告

    SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...

  7. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  8. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

    1352: New Sorting Algorithm Time Limit: 1 Sec  Memory Limit: 128 MB Description We are trying to use ...

随机推荐

  1. JS中的闭包(转自他处)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  2. 北京大学Cousera学习笔记--6-计算导论与C语言基础--计算机的基本原理-认识程序设计语言 如何学习

    1.是一门高级程序语言 低级语言-机器语言(二进制) 汇编语言-load add save mult 高级语言:有利于人们编写理解 2.C语言的规范定义非常的宽泛 1.long型数据长度不短于int型 ...

  3. JAVA常用加密解密算法Encryption and decryption

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  4. 安装rlwrap-0.37.tar.gz

    1.解压下载好的rlwrap文件 [root@wangliping tool]# tar -zxvf rlwrap-0.37.tar.gz 2.进入解压好的文件[root@wangliping too ...

  5. 用友畅捷通T6数据升级到T+的步骤图解

    1.打开升级工具,输入各项目: 2.点击下一步并确认---“是”: 3.输入目的账套信息(主要是系统管理员密码): 4.升级开始: 5.退出原账套登录的确认----“是”: 6.升级进度: 7.升级成 ...

  6. 记录tiny6410 使用linux-2.6.28.6内核遇到starting kernel...的问题

    1\问题的解决主要是参考了tiny210相同的问题 2\/home/suxuandong/Documents/computer/linux_kernel/tiny6410/linux-2.6.28.6 ...

  7. rabbitMq交换机direct、topics

    一: direct 上面我用采用了广播的模式进行消息的发送,现在我们采用路由的方式对不同的消息进行过滤 发送端代码 public class RoutingSendDirect { private s ...

  8. Jmeter上传附件EXCEL

    1.通过对上传附件接口进行抓包,获取的信息如下: 2.在jmeter脚本中添加http请求,并添加http请求头信息如下: 3.在http请求中添加上传附件的内容如下,由于我上传的是excel,所以M ...

  9. js中,转义字符的表示

    HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希望在网页中显示这些符号, ...

  10. .NET Framework简介

    NET Framework 就是微软Web Services 引擎1.NET Framework 旨在实现下列目标:提供一个一致的面向对象的编程环境,而无论对象代码是在本地存储和执行,还是在本地执行但 ...