博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bubble sort of sorting algorithm
阅读量:5993 次
发布时间:2019-06-20

本文共 1945 字,大约阅读时间需要 6 分钟。

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 
>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
{
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.

 

 

 

 

转载于:https://www.cnblogs.com/norahc/p/9693962.html

你可能感兴趣的文章
sysbench测试服务器性能
查看>>
复合非聚集索引——列顺序重要么?
查看>>
在 Linux 中安装 Lighttpd Web 服务器
查看>>
C#动态调用WCF接口(3)
查看>>
MySQL · 引擎特性 · InnoDB 事务子系统介绍
查看>>
html5调用摄像头实现拍照
查看>>
org.springframework.context.event.AbstractApplicationEventMulticaster
查看>>
C#操作符??和?:
查看>>
Css背景
查看>>
nginx相关优化
查看>>
spring源码:Aware接口
查看>>
SQL练习之两个列值的交换
查看>>
Cross-Origin Resource Sharing协议介绍
查看>>
Win2008 Server系统安装打印服务器与配置
查看>>
JAX-RS开发 hello world
查看>>
题目:请实现一个函数,将一个字符串中的空格替换成“%20”
查看>>
shell 的语法
查看>>
第三章 logstash - 输入插件之tcp与redis
查看>>
组件名简单改了
查看>>
weekend110(Hadoop)的 第三天笔记
查看>>