Bubble

作者: 賈小強 | 来源:发表于2018-03-28 11:05 被阅读1次

简书 賈小強
转载请注明原创出处,谢谢!

package com.lab1.test2;

public class Bubble {
    public static void main(String[] args) {
        Comparable[] a = { 5, 3, 1, 2, 4 };
        sort(a);
        show(a);
    }

    private static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }

    private static void sort(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 1; j < a.length - i; j++) {
                if (less(a, j, j - 1)) {
                    exch(a, j - 1, j);
                }
            }
        }
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    private static boolean less(Comparable[] a, int i, int j) {
        return a[i].compareTo(a[j]) < 0;
    }
}

输出

1 2 3 4 5 

Happy learning !!

相关文章

  • Economist 20161024

    1. When a bubble is not a bubble smack of something: to h...

  • Bubble

    简书 賈小強转载请注明原创出处,谢谢! 输出 Happy learning !!

  • Bubble系列服装设计说明

    Bubble(泡泡)系列服装款式图 Bubble系列服装效果图 此次毕业设计主题定为:Bubble。 眼睛往往惊奇...

  • 50.When a bubble is not a bubble

    今天霞姐讲课所以就很开心,原来她只讲经济学人的文章。 必备 property market['prɒpətɪ]房地...

  • Launch Fish Bubble(Launch Fish B

    Launch Fish Bubble

  • Bubble Sort

    Bubble Sort Code

  • Bubble Sort

    Given an array of integers, sort the elements in the arra...

  • Bubble Run

    浪了个浪!

  • Bubble Sort

    冒泡排序,是不断地比较相邻的两个元素,较小的像上冒,较大的向下沉,排序的过程如同水中气泡上升,即两两比较相...

  • Bubble Shooter

    Superfunbubbleblust! Specialeffectsgorgeous,veryexciting ...

网友评论

      本文标题:Bubble

      本文链接:https://www.haomeiwen.com/subject/dwhycftx.html