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 ...
随机推荐
- springMVC--XML解析
一 springMVC 入口 web.xml; DispatcherServlet二 初始化过程 1.寻找init(); 查看DispatcherServlet时候时,继承自servlet,肯定有初始 ...
- arcgis 画图工具
arcgis 4.x系列JavaScript API在4.4及以前的版本对画图工具(Draw)是不支持的,自4.5版本后才提供Draw的类.相关连接:https://developers.arcgis ...
- 1.1大数据平台架构及Hadoop生态圈
1.硬件架构实例 2.软件架构实例 3.数据流通用概念模型 a.数据源(互联网.物联网.企业数据):App.Device.Site b.数据收集(ETL.提取.转换.加载):Flume.Kafka.S ...
- 北京大学Cousera学习笔记--7-计算导论与C语言基础--基本数据类型&变量&常量
1.整形数据 1.基本型(int 4B).短整型(short 2B).长整型(long 4B) VC环境下 sizeof运算符用于计算某种类型的对象在内存中所占的字节数 ,用法:size(int) ...
- android sdk 安装 配置
下载android sdk manager:http://dl.google.com/android/installer_r24.4.1-windows.exe 打开sdk manager 在tool ...
- redis知识点汇总
1. redis是什么 2. 为什么用redis 3. redis 数据结构 4. redis中的对象类型 5. redis都能做什么?怎么实现的的? 6. redis使用过程中需要注意什么 7. 数 ...
- [ABP] ASP.NET Zero 5.6.0 之 ASP.NET Zero Power Tools 上手日志
之前破解了这个工具后,却没有使用它. 现在使用这个小工具,帮我完成创建Entity类,Dto类,AppService类,View视图等DDD相关工作以及Entity Framework Migrati ...
- Character
Character a = new Character(); Character.isUpperCase(a) 判断给点的字符是否是大写字符 Character.isLowerCase(a) 判断给定 ...
- 【sed】增加一列【shell文本处理】
有些简单的文本处理不需要写程序,利用awk和sed就可以很好的完成. 今天记录一下在已有文件中增加一列的方法 sed -i "s/^/Chr${i}\t&/g" file ...
- 在linux上安装python, jupyter, 虚拟环境(virtualenv)以及 虚拟环境管理之virtualenvwraper
一, 安装python31.下载python3源码 wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz2.解压缩源码包,去 ...