Bubble sort of sorting algorithm
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的更多相关文章
- POJ3761 Bubble Sort (组合数学,构造)
题面 Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be ...
- 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)
http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 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 ...
- 「SP25784」BUBBLESORT - Bubble Sort 解题报告
SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...
- Bubble Sort (5775)
Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
随机推荐
- 软件工程first homework
1) 2017*****7193:我是最乐观的刘新飞:我的爱好是下中国象棋和听音乐: 我的码云个人主页是码云个人主页: 我的第一个项目地址是×××: 自己目前的代码量是三千行左右:我最喜欢蛋肠炒面(一 ...
- javaweb 发布目录
一个Java Web项目要运行,它首先要放在tomcat之类的容器中:该JavaWeb项目的构成一定要包含下面几种文件以及文件夹: META-INF : 存放一些meta information相关的 ...
- NuGet的简单使用
什么是NuGet? NuGet(读作New Get)是用于微软.NET开发平台的软件包管理器,是一个Visual Studio的扩展.在使用Visual Studio开发基于.NET Framewor ...
- CentOS 7 FTP环境部署
FTP协议有两种工作方式: 1)port方式:主动模式 port(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求 , 服务器接受连接 , 建立一条命令链路 当需要传送数据 ...
- JavaScript之基础语法整理
1.数据类型(number,boolean,string,null,undefined,symbol,object) es是动态语言,弱类型语言,虽然先声明了变量,但是变量可以重新赋值任意类型 弱类型 ...
- Oracle中row_number()、rank()、dense_rank() 的区别
link:https://www.cnblogs.com/qiuting/p/7880500.html
- clean exit - waiting for changes before restart
在使用nodemon的时候,针对于同一个文件一次使用还好,当多次使用的时候就会出现这样的情况: 解决办法: nodemon clean exit 原因: 可能是因为缓存造成的:
- 叮咚,你的Lauce上线了!
哈,2014 - 2016 - 2018,虽然每隔两年才有那么一篇随笔,博客园,我还是爱你的~ 嗯,2018,马上又要失业了,我这是自带黑属性啊啊啊哈,工作了4年多的项目要被砍掉了, 倒不是说非要这个 ...
- LeetCode Weekly Contest 121
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers ...
- Unity 个人用过的地面检测方案总结
Unity 个人用过的地面检测方案总结 1.普通射线 在角色坐标(一般是脚底),发射一根向下的射线,长度大约为0.2, 只适用于简单地形,实际使用中常常遇到以下问题 用的collider去碰撞地面时, ...