Java冒泡排序算法

时间:2021-11-15 12:57:25
Java冒泡排序算法 冒泡排序算法详解:http://blog.csdn.net/ysjian_pingcx/article/details/8653732 冒泡排序算法源码免积分下载:http://download.csdn.net/detail/ysjian_pingcx/6755209
序:一个爱上Java最初的想法一直没有磨灭:”分享我的学习成果,不管后期技术有多深,打好基础很重要“。
声明:所有源码经由本人原创,上道者皆似曾相识,另未经过权威机构或人士审核和批准,勿做商业用途,仅供学习分享,若要转载,请注明出处。

工具类Swapper,后期算法会使用这个工具类:
package com.meritit.sortord.util;

/**
* One util to swap tow element of Array
*
* @author ysjian
* @version 1.0
* @email ysjian_pingcx@126.com
* @QQ 646633781
* @telephone 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 2013-12-20
* @copyRight Merit
*/
public class Swapper {

private Swapper() {

}

/**
* Swap tow elements of the array
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param array
* the array to be swapped
* @exception NullPointerException
* if the array is null
*/
public static <T extends Comparable<T>> void swap(int oneIndex,
int anotherIndex, T[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
T temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}

/**
* Swap tow elements of the array
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param array
* the array to be swapped
* @exception NullPointerException
* if the array is null
*/
public static void swap(int oneIndex, int anotherIndex, int[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
int temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}

/**
* Check the index whether it is in the arrange
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param arrayLength
* the length of the Array
* @exception IllegalArgumentException
* if the index is out of the range
*/
private static void checkIndexs(int oneIndex, int anotherIndex,
int arrayLength) {
if (oneIndex < 0 || anotherIndex < 0 || oneIndex >= arrayLength
|| anotherIndex >= arrayLength) {
throw new IllegalArgumentException(
"illegalArguments for tow indexs [" + oneIndex + ","
+ oneIndex + "]");
}
}
}
冒泡排序算法BubbleSortord:
package com.meritit.sortord.bubble;

import com.meritit.sortord.util.Swapper;

/**
* Bubble sort order, time complexity is O(n2)
*
* @author ysjian
* @version 1.0
* @email ysjian_pingcx@126.com
* @QQ 646633781
* @telephone 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 2013-12-20
* @copyRight Merit
* @since 1.5
*/
public class BubbleSortord {

private static final BubbleSortord INSTANCE = new BubbleSortord();

private BubbleSortord() {
}

/**
* Get the instance of ComparisonSortord, only just one instance
*
* @return the only instance of BubbleSortord
*/
public static BubbleSortord getInstance() {
return INSTANCE;
}

/**
* Sort the array of <code>int</code> with bubble sort order
*
* @param array
* the array of int
*/
public void doSort(int... array) {
int length = array.length;

// a flag judge whether the array is in order
boolean flag = true;
for (int i = 0; i < length - 1 && flag; i++) {
flag = false;// set false
for (int j = 0; j < length - i - 1; j++) {
if (array[j] > array[j + 1]) {

// compare two neighbor elements
Swapper.swap(j, j + 1, array);
/*
* if the line is not reach, indicate that the array is in
* order, then break the circulation
*/
flag = true;
}
}
}
}

/**
* Sort the array of generic <code>T</code> with bubble sort order
*
* @param array
* the array of generic <code>T</code>
*/
public <T extends Comparable<T>> void doSortT(T[] array) {
int length = array.length;
boolean flag = true;
for (int i = 0; i < length - 1 && flag; i++) {
flag = false;
for (int j = 0; j < length - i - 1; j++) {
if (array[j].compareTo(array[j + 1]) > 0) {
Swapper.swap(j, j + 1, array);
flag = true;
}
}
}
}
}
测试TestBubbleSortord:
package com.meritit.sortord.bubble;

import java.util.Arrays;

public class TestBubbleSortord {
/**
* Test
*
* @param args
*/
public static void main(String[] args) {
BubbleSortord bubbleSort = BubbleSortord.getInstance();
int[] array = { 25, 36, 21, 45, 98, 13 };
System.out.println(Arrays.toString(array));
bubbleSort.doSort(array);
System.out.println(Arrays.toString(array));
System.out.println("------------------------");
Integer[] arrays = { 25, 35, 11, 45, 98, 65 };
System.out.println(Arrays.toString(arrays));
bubbleSort.doSortT(arrays);
System.out.println(Arrays.toString(arrays));
}
}
冒泡排序算法详解:http://blog.csdn.net/ysjian_pingcx/article/details/8653732冒泡排序算法源码免积分下载:http://download.csdn.net/detail/ysjian_pingcx/6755209