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. #WEB安全基础 : HTTP协议 | 文章索引

    本系列讲解WEB安全所需要的HTTP协议 #WEB安全基础 : HTTP协议 | 0x0 TCP/IP四层结构 #WEB安全基础 : HTTP协议 | 0x1 TCP/IP通信 #WEB安全基础 : ...

  2. VS2008生成解决方案卡顿、龟速

    1.工具-选项-项目和解决方案-MS BUILD 项目生成输出详细信息中选择“诊断” 2.进入.NET环境的安装位置:C:\WINDOWS\Microsoft.NET\Framework\v3.5 , ...

  3. vuepress 学习心得

    vuepress是一个静态网站生成器,在我看来就是写博客和教程的好工具.教程请见官网:https://www.vuepress.cn 安装方法建议局部安装:node8.0以上,新建vue项目,可能会出 ...

  4. 在vue中使用echarts图表

    在vue中使用echarts图表   转载请注明出处:https://www.cnblogs.com/wenjunwei/p/9815290.html 安装vue依赖 使用npm npm instal ...

  5. UEP-添加

    添加数据时候自动更新时间,注意添加数据时,要设置最大主键 时间的工具类:CommonUtil 人的工具类: ContextUtil ScmCompanyAccount scmCompanyAccoun ...

  6. Tensorflow --BeamSearch

    github:https://github.com/zle1992/Seq2Seq-Chatbot 1. 注意在infer阶段,需要需要reuse, 2.If you are using the Be ...

  7. UnityWebRequest http post json通信

    unity3d 提供了一个用于http通信的类叫:UnityWebRequest,它是www的替代者,所以建议使用这个类.我们这个例子以json格式与服务器通信.这里使用的json组件是:Newton ...

  8. ajax请求网络api

    不啰嗦,直接上代码: 1.在浏览器输入网址:http://api.asilu.com/weather/?callback=getname&city=深圳 你会看到如下结果:他返回的是json数 ...

  9. C#导出文本内容到word文档源码

    将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...

  10. [最新]ABP ASP.NET Zero v5.5.2 破解

    参考https://www.cnblogs.com/xajh/p/8428818.html后, 搞定了Abp.AspNetZeroCore.dll.Abp.AspNetZeroCore.Web.dll ...