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. luogu P3197 [HNOI2008]越狱

    构造长度为n的串,给定m种颜色,求使得相邻两位的颜色相同的方案数 显然可以看出长度为n的串染m种颜色的总方案数为$m^{n}$ 然后来考虑相邻两位颜色不同的方案 对于第一位,有m种选择 对于剩余的n- ...

  2. Java 多线程之Timer与ScheduledExecutorService

    1.Timer管理延时任务的缺陷 a.以前在项目中也经常使用定时器,比如每隔一段时间清理项目中的一些垃圾文件,每个一段时间进行数据清洗:然而Timer是存在一些缺陷的,因为Timer在执行定时任务时只 ...

  3. VC6中函数点go to definition报告the symbol XXX is undefined

    删除Debug中的bsc文件,再重建所有文件即可,在该函数处点击go to definition会提示重建.bsc文件,如果不行,多操作几次.

  4. Blender学习

    学习顺序(下面为引用他人的视频或博客) 51个必须知道的blender操作 https://www.bilibili.com/video/av4619930/ Blender常用快捷键一览表 http ...

  5. python中split()的用法

    Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串. 语法: str.split(str="", num=str ...

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

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

  7. SQLServer 2008以上误操作数据库恢复方法

    解决方法:       对于这类问题,主要是找回误操作之前的数据,在2008之前,有个很出名的工具Log Exploer,听说还挺好用的,这个网上大把教程,这里就不多说了.但是唯一遗憾的是,不支持20 ...

  8. hbase的一些要点

    hbase特点及简介: hbase源自于谷歌的三大论文之一 GFS -- hdfs MapReduce - MR BigTable - hbase hbase在以Hadoop为基础的生态圈中的地位 h ...

  9. Java8比较器(Lamdba)

    1.首先构造一个实体以便示例使用 public class Developer { private String name; private BigDecimal salary; private in ...

  10. flutter 读写文件

    import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'd ...