好记性不如烂笔头,仅以记之
排序接口
/**
* 排序接口
* @author kongge
*
*/
public interface ISort {
public void sort(int[] numbers);
}
/**
* 数据接口
* @author kongge
*
*/
public interface ISortConstants {
public static final int TYPE_SORT_BUBBLE = 1; // 冒泡排序
public static final int TYPE_SORT_BUCKET = 2; // 桶排序
public static final int TYPE_SORT_QUICK = 3; // 快排
}
/**
* 排序简单工厂
* @author kongge
*
*/
public class SortFactory {
public static ISort createSort(int type) {
ISort sort = null;
switch (type) {
case ISortConstants.TYPE_SORT_BUBBLE:
sort = new BubbleSort();
break;
case ISortConstants.TYPE_SORT_BUCKET:
sort = new BucketSort();
break;
case ISortConstants.TYPE_SORT_QUICK:
sort = new QuickSort();
break;
default:
break;
}
return sort;
}
}
一:桶排序
/**
*
* @author kongge
* @descrition 桶排序
* 桶排序是最快的排序算法,时间复杂度为O(1),但是简单的桶排序只能排一定范围内的非负数
*/
public class BucketSort implements ISort {
public BucketSort() {
}
@Override
public void sort(int[] numbers) {
int size = numbers.length;
if (size <= 0) {
System.out.println("数量不能为空");
return;
}
// 找到最大的数
int maxNum = numbers[0];
for (int i = 0; i < numbers.length; i++) {
int num = numbers[i];
if (num < 0) {
System.out.println("只支持非负数的排序");
return;
}
maxNum = maxNum > num ? maxNum : num;
}
// 开辟maxNum + 1大的空间,
int[] sortArray = new int[maxNum + 1];
// 将对应的数装入下标为该数的桶
for (int i = 0; i < size; i++) {
int num = numbers[i];
sortArray[num] ++;
}
System.out.println("---after sort-----");
int sortArraySize = sortArray.length;
for (int i = 0; i < sortArraySize; i++) {
for (int j = 1; j <= sortArray[i]; j++) {
System.out.print(" " + i);
}
}
System.out.println("\n---end------");
}
}
二:冒泡排序
/**
*
* @author kongge
* @description 冒泡排序
* 时间复杂度O(n^2)
* 将第一个数依次跟后面的数比较,如果比后面的大,则交换,一直比到size - i - 1(优化,不必每次比到最后)
*/
public class BubbleSort implements ISort{
public BubbleSort() {
}
@Override
public void sort(int[] numbers) {
if (numbers.length <= 1) {
return;
}
System.out.println("---start sort----------");
int size = numbers.length;
for (int i = 0; i < size; i++) {
for (int j = 0; j < (size - i - 1); j++) {
int first = numbers[j];
int second = numbers[j + 1];
int temp = 0;
if (first > second) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
for (int i = 0; i < size; i++) {
System.out.print(" " + numbers[i]);
}
System.out.println("\n---end sort----------");
}
三:快速排序
/**
* 快排
*
* @author kongge
* 时间复杂度O(nlogn)
*
*/
public class QuickSort implements ISort {
public QuickSort() {
}
@Override
public void sort(int[] numbers) {
if (numbers == null || numbers.length <= 1) {
return;
}
System.out.println("快速排序:");
sortLeftAndRight(numbers, 0, numbers.length - 1);
}
private void sortLeftAndRight(int[] numbers, int left, int right) {
if (left >= right) {
return;
}
int standeredNum = numbers[left];
int leftPoint = left;
int rightPoint = right;
while (leftPoint < rightPoint) {
while (leftPoint < rightPoint && numbers[rightPoint] >= standeredNum) {
rightPoint--;
}
while (leftPoint < rightPoint && numbers[leftPoint] <= standeredNum) {
leftPoint++;
}
if (leftPoint != rightPoint) {
int temp = numbers[leftPoint];
numbers[leftPoint] = numbers[rightPoint];
numbers[rightPoint] = temp;
}
}
int temp = numbers[leftPoint];
numbers[leftPoint] = numbers[left];
numbers[left] = temp;
sortLeftAndRight(numbers, left, leftPoint - 1);
sortLeftAndRight(numbers, leftPoint + 1, right);
}
}
使用:
public class Client {
public static void main(String[] args) {
int[] array = {0, 2, 8, 77, 3, 9, 8, 3, 1, 3, 4, 3};
sortAndPrint(array, ISortConstants.TYPE_SORT_QUICK);
}
private static void sortAndPrint(int[] numbers, int sortType) {
ISort sort = SortFactory.createSort(sortType);
if (sort == null) {
System.out.println("请指定一种排序方式");
return;
}
System.out.println("---start sort-----");
sort.sort(numbers);
int size = numbers.length;
for (int i = 0; i < size; i++) {
System.out.print(" " + numbers[i]);
}
System.out.println("\n---end sort-----");
}
}