概述

/**
* @author HansChen
*/
public class Sorter {

/**
* 冒泡排序
*/
public int sort(int[] array) {
int operations = 0;
if (array.length <= 1) {
return operations;
}

for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
operations++;
if (needSwap(array, j)) {
swap(array, j);
}
}
}

return operations;
}

/**
* @return 是否需要交换数组中 index 和 index+1 元素
*/
private boolean needSwap(int[] array, int index) {
return array[index] > array[index + 1];
}

/**
* 交换array数组中的 index 和 index+1 元素
*/
private void swap(int[] array, int index) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}

模板方法模式

/**
* @author HansChen
*/
public abstract class BubbleSorter<T> {

/**
* 冒泡排序
*/
public int sort(T array) {

setArray(array);
int length = getLength();

int operations = 0;
if (length <= 1) {
return operations;
}

for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
operations++;
if (needSwap(j)) {
swap(j);
}
}
}

return operations;
}

/**
* 初始化排序数组
*/
protected abstract void setArray(T array);

/**
* @return 返回数组长度
*/
protected abstract int getLength();

/**
* @return 是否需要交换数组中 index 和 index+1 元素
*/
protected abstract boolean needSwap(int index);

/**
* 交换array数组中的 index 和 index+1 元素
*/
protected abstract void swap(int index);
}

public class IntBubbleSorter extends BubbleSorter<int[]> {

private int[] array;

@Override
protected void setArray(int[] array) {
this.array = array;
}

@Override
protected int getLength() {
return array == null ? 0 : array.length;
}

@Override
protected boolean needSwap(int index) {
return array != null && (array[index] > array[index + 1]);
}

@Override
protected void swap(int index) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}

public class DoubleBubbleSorter extends BubbleSorter<double[]> {

private double[] array;

@Override
protected void setArray(double[] array) {
this.array = array;
}

@Override
protected int getLength() {
return array == null ? 0 : array.length;
}

@Override
protected boolean needSwap(int index) {
return array != null && (array[index] > array[index + 1]);
}

@Override
protected void swap(int index) {
double temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}

public class IntegerListBubbleSorter extends BubbleSorter<List<Integer>> {

private List<Integer> list;

@Override
protected void setArray(List<Integer> list) {
this.list = list;
}

@Override
protected int getLength() {
return list == null ? 0 : list.size();
}

@Override
protected boolean needSwap(int index) {
return list != null && (list.get(index) > list.get(index + 1));
}

@Override
protected void swap(int index) {
int temp = list.get(index);
list.set(index, list.get(index + 1));
list.set(index + 1, temp);
}
}

public class Test {

public static void main(String[] args) {

//对整型数组排序
int[] intArray = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int operations = new IntBubbleSorter().sort(intArray);
System.out.println("[Template Method] operations:" + operations + ", array:" + Arrays.toString(intArray));

//对double数组排序
double[] doubleArray = {9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0};
operations = new DoubleBubbleSorter().sort(doubleArray);
System.out.println("[Template Method] operations:" + operations + ", array:" + Arrays.toString(doubleArray));

//对List集合排序
List<Integer> list = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
operations = new IntegerListBubbleSorter().sort(list);
System.out.println("[Template Method] operations:" + operations + ", list:" + list.toString());
}
}

策略模式

public class BubbleSorter<T> {

/**
* 抽象策略接口，可以有不同的实现
*/
private SortHandler<T> sortHandler;

public BubbleSorter(SortHandler<T> sortHandler) {
this.sortHandler = sortHandler;
}

/**
* 冒泡排序
*/
public int sort(T array) {

sortHandler.setArray(array);
int length = sortHandler.getLength();

int operations = 0;
if (length <= 1) {
return operations;
}

for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
operations++;
if (sortHandler.needSwap(j)) {
sortHandler.swap(j);
}
}
}

return operations;
}
}

public interface SortHandler<T> {

/**
* 初始化排序数组
*/
void setArray(T array);

/**
* @return 返回数组长度
*/
int getLength();

/**
* @return 是否需要交换数组中 index 和 index+1 元素
*/
boolean needSwap(int index);

/**
* 交换array数组中的 index 和 index+1 元素
*/
void swap(int index);
}

public class IntSortHandler implements SortHandler<int[]> {

private int[] array;

@Override
public void setArray(int[] array) {
this.array = array;
}

@Override
public int getLength() {
return array == null ? 0 : array.length;
}

@Override
public boolean needSwap(int index) {
return array != null && (array[index] > array[index + 1]);
}

@Override
public void swap(int index) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}

public class DoubleSortHandler implements SortHandler<double[]> {

private double[] array;

@Override
public void setArray(double[] array) {
this.array = array;
}

@Override
public int getLength() {
return array == null ? 0 : array.length;
}

@Override
public boolean needSwap(int index) {
return array != null && (array[index] > array[index + 1]);
}

@Override
public void swap(int index) {
double temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}

public class IntegerListSortHandler implements SortHandler<List<Integer>> {

private List<Integer> list;

@Override
public void setArray(List<Integer> list) {
this.list = list;
}

@Override
public int getLength() {
return list == null ? 0 : list.size();
}

@Override
public boolean needSwap(int index) {
return list != null && (list.get(index) > list.get(index + 1));
}

@Override
public void swap(int index) {
int temp = list.get(index);
list.set(index, list.get(index + 1));
list.set(index + 1, temp);
}
}

public class Test {

public static void main(String[] args) {
//对整型数组排序
int[] intArray = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
BubbleSorter<int[]> intBubbleSorter = new BubbleSorter<>(new IntSortHandler());
int operations = intBubbleSorter.sort(intArray);
System.out.println("[Strategy] operations:" + operations + ", array:" + Arrays.toString(intArray));

//对double数组排序
double[] doubleArray = {9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0};
BubbleSorter<double[]> doubleBubbleSorter = new BubbleSorter<>(new DoubleSortHandler());
operations = doubleBubbleSorter.sort(doubleArray);
System.out.println("[Strategy] operations:" + operations + ", array:" + Arrays.toString(doubleArray));

//对List集合排序
List<Integer> list = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
BubbleSorter<List<Integer>> integerListBubbleSorter = new BubbleSorter<>(new IntegerListSortHandler());
operations = integerListBubbleSorter.sort(list);
System.out.println("[Strategy] operations:" + operations + ", list:" + list);
}
}

总结

02-23 1529

03-25 8342

03-09 54万+

07-14 171

11-03 1410

04-22 83

08-14 281

01-22 92

02-24 966

08-02 552

08-25 1474

11-01 766

08-25 1680

03-02 182

12-11 283

07-21 797

09-17 304

08-29 239

03-28 229

06-18 1884

04-10 748

07-07 48

04-11 4万+

04-03 9066

04-20 4万+

05-16 7340

04-16 3万+

03-23 1万+

04-06 7万+

12-05 10

05-17 9306

04-07 5万+

04-11 9019

04-18 4万+

04-15 6万+

04-09 8万+

04-11 5万+

03-28

03-21

04-14 59万+

03-13 15万+

03-04 14万+

03-08 2万+

04-25 7万+

03-10 13万+

03-10 18万+

03-12 11万+

03-13 11万+

03-18 1万+

03-19 8万+

03-19 3万+

03-22 4万+

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 22万+

03-29 10万+

03-30 17万+

06-01 6908

06-01 1万+

04-02 1万+

04-02 6379

04-02 4万+

04-06 1万+

04-09 3万+

04-24 3万+

05-01 6750

05-03 1万+

05-16 5万+

05-06 1万+

05-07 4099

05-07 1万+

05-08 4万+

05-10 3727

05-14 7063

05-16 3978

如何用Python提高办公（Excel）效率？

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客