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 ...
随机推荐
- 设置VS2017背景图片
设置方法很简单:安装扩展ClaudiaIDE 1.在这里下载扩展,https://visualstudiogallery.msdn.microsoft.com/9ba50f8d-f30c-4e33-a ...
- docker容器实战-----初级<2>
第二章 docker容器 1. Docker是通过内核虚拟化技术(namespaces及cgroups cpu.内存.磁盘io等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚 ...
- js大图轮播和倒计时
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hibernate一级缓存及对象的状态
hibernate中实体类对象的状态 在hibernate中实体类对象有三种状态 (1)瞬时态(临时态) 瞬时态:即我们自己创建一个对象,还没有保存到数据库就叫临时态,其实也可以说是对像没有id值,跟 ...
- Promise的那些事儿
在JavaScript中,异步操作非常多见,然而在Promise之前,我们是在类似以下的做法中处理多重异步回调,每一层里都要调另一个异步函数,形成了所谓的"回调地狱", Promi ...
- 福州大学软件工程1916 | W班 作业成绩排名汇总
评分链接: 第一次作业-准备篇 https://www.cnblogs.com/deerCode/p/10527237.html 第二次作业-结对第一次-文献摘要热词统计原型设计 https://ww ...
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165215
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165215 目录 Kali的下载及安装 网络配置 设置共享文件夹及剪切板 更新软件源 [Kali的下载及安 ...
- IDEA 热部署设置(JRebel插件激活)
参考原文链接:https://blog.csdn.net/feidi7783/article/details/80607374
- 【源码】HashMap源码及线程非安全分析
最近工作不是太忙,准备再读读一些源码,想来想去,还是先从JDK的源码读起吧,毕竟很久不去读了,很多东西都生疏了.当然,还是先从炙手可热的HashMap,每次读都会有一些收获.当然,JDK8对HashM ...
- OLTP和OLAP的区别
OLTP和OLAP的区别 联机事务处理OLTP(on-line transaction processing) 主要是执行基本日常的事务处理,比如数据库记录的增删查改.比如在银行的一笔交易记录,就是一 ...