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 ...
随机推荐
- vue生命周期钩子函数
<template> <div> <h2>this is from C.vue</h2> </div> </template> ...
- php中 "$$" 的详解
php中 $$str 这种写法称为可变变量 有时候使用可变变量名是很方便的.就是说,一个变量的变量名可以动态的设置和使用.一个普通的变量通过声明来设置,例如: <?php $a = " ...
- 按Enter登录,Esc退出
回车登录,下图处,选择登录按钮的方法 下图是按ESC
- redis 在 php 中的应用(key篇)
本文为我阅读了 redis参考手册 之后结合 博友的博客 编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) 目录: KEY(键) DEL ...
- 'scope' is defined but never used
错误如下: 解决办法: 1.scope这个属性在最新版本vue已经被弃用,升级成slot-scope了 ,所以属性名应该改为slot-scope. 2.如上所示,我们发现,还是有报错,原因是vetur ...
- Linux监控工具讲解
本文主要记录一下 Linux系统上一些常用的系统监控工具,非常好用.正所谓磨刀不误砍柴工,花点时间总结一下是值得的! 本文内容脑图如下: top 命令 top 命令我想大家都挺熟悉吧!Linux 下的 ...
- SQL查询数据时报错
在开发过程中如果查询报如下的错误: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column ...
- C语言实例:结构体
结构体: #include <stdio.h> #include <stdlib.h> //#pragma pack(1) typedef struct{ short i; / ...
- flutter sqflite
https://www.jianshu.com/p/88998af66e4b https://www.jianshu.com/p/7ac3ce2bc0c6
- Bugku-CTF之welcome to bugkuctf(php://filter和php://input的妙用)
Day24 welcome to bugkuctf http://123.206.87.240:8006/test1/ 本题要点:代码审计,PHP://filter , php://input , ...