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 ...
随机推荐
- 用原生js+canvas实现五子棋
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- H.264学习--1
1.宏块(Macro Block):一个编码图像首先要划分成多个块(4x4 像素)才能进行处理,显然宏块应该是整数个块组成,通常宏块大小为 ...
- Install rapyuta client on Raspberry Pi
Install rapyuta on client sudo git clone -b master https://github.com/cnsdytzy/-Rapyuta-installation ...
- jquery的cookie插件
一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...
- tomcat部署项目
提示:指定jdk版本 在bin路径下的setclasspath.bat文件添加 set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_201 set JRE_HO ...
- ltp-ddt nor qspi spi调试中需要修改的地方
1 blk_device_dd_readwrite_test.sh before SRC_FILE="/home/root/srctest_file_${DEVICE_TYPE}_$$&qu ...
- Git_GitHub-使用过程遇到的问题——坑(持续添加)
push错误——>master git push -u origin master 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git remote rm origin 2.再添加 ...
- 大量的rcuob进程
环境: OS:Centos 7 问题,今天采购了一台dell R430机器,启动发现大量的如下进程[root@localhost opt]# toptop - 02:07:57 up 6:39, 2 ...
- JS版剑指offer
介绍 用JavaScript刷完了剑指offer,故总结下每道题的难度.解决关键点,详细题解代码可以点链接进去细看. 关于JS刷题的技巧可以看我之前的这篇:JS刷题总结. 剑指offer的题目在牛客网 ...
- 关于SQL优化的一点建议
前段时间一直在做关于性能优化相关的工作,结合自己的实际工作经验,只针对SQL层面提一些优化的规范和建议. 针对SQL编写 1.单条SQL长度不宜超过100行: 2.SQL子查询不宜嵌套3层: 子查询嵌 ...