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. SVN的安装和启动SVN的安装

    SVN的安装和启动SVN的安装 i. windows下安装SVN 首先要先下载SVN服务器,下载地址https://www.visualsvn.com/downloads/ 下载软件VisualSVN ...

  2. (.NET高级课程笔记)泛型总结

    泛型总结 1.引入泛型:延迟声明,即在声明的时候没有指定参数类型,只有当调用的时候才会确定 其参数类型(架构师的理念:推迟一切可以推迟的) 2.如何声明和使用泛型 3.泛型的好处和原理 4.泛型类.泛 ...

  3. IDEA 201809 Jrebel安装破解

    jrebel介绍: JRebel是一款JAVA虚拟机插件,它使得JAVA程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响.JRebel使你能即时分别看到代码.类和资源的变化, ...

  4. 解决CentOS(6和7版本),/etc/sysconfig/下没有iptables的问题

    一.Centos 6版本解决办法: 1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service ip ...

  5. 【转】2、Jenkins构建完成自动发送邮件

    1.开通163邮箱的授权码服务,和SMTP服务.百度找教程.2.安装 Email Extension Plugin 插件,已安装或版本自带可跳过此步骤.3.进入系统管理–系统设置首先配置 Jenkin ...

  6. spring-boot 2.1.2.RELEASE bug

    不打印映射地址日志: 改用 2.0.5 RELEASE 即可

  7. 如何在linux上查看tomcat的端口号

    1.先到tomcat配置文件查看tomcat的端口是什么,配置文件一般是:$CATALINA_HOME/conf/server这个文件,查找<Connector port="8080& ...

  8. SQL进阶1:case表达式的用法示例

    一:case表达式的用法 1.SQL中的case表达式的作用是用来对"某个变量"进行某种转化,通常在select字句中使用,举个例子: 不能看出,case表达式很像我们的if el ...

  9. 错误:软件包:3:docker-ce-18.09.4-3.el7.x86_64 (docker-ce-stable) 需要:container-selinux >= 2.9

    命令:yum -y install http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.68-1. ...

  10. IDEA Failed to load dx.jar

    IDEA-177053 Android app crashes on build "Failed to load dx.jar" Error:Android Pre Dex: [c ...