java8 函数式版 冒泡排序(BubbleSort)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.doctor.algorithm.sort;

import java.util.Arrays;
import java.util.function.BiPredicate;

/**
 * java8 函数式版 冒泡排序(BubbleSort)
 * 
 * @author doctor
 *
 * @time 2015年4月26日 下午9:31:03
 */
public class BubbleSort {

	public static void main(String[] args) {
		Long[] array = { 33L, 55L, 2L, 4L, 77L, 66L };
		sort(array, (a, b) -> Long.compare(a, b) > 0);
		System.out.println(Arrays.asList(array));

	}

	private static  void sort(T[] array, BiPredicate biPredicate) {
		for (int i = array.length - 1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				if (biPredicate.test(array[j], array[i])) {
					T temp = array[j];
					array[j] = array[i];
					array[i] = temp;
				}
			}
		}

	}
}


转载于:https://my.oschina.net/doctor2014/blog/406560

你可能感兴趣的:(java8 函数式版 冒泡排序(BubbleSort))