Dan*_*tex 2 java sorting methods merge quicksort
我正在对排序算法进行一些测量测试.
我创建了这个方法来计算选择排序需要订购数组的时间
public static double timeToSelectionSort(Double[] arrayOfNumbers) {
double timeToSelectionSort =0;
Stopwatch stopwatch = new Stopwatch();
Selection.sort(arrayOfNumbers);
timeToSelectionSort = stopwatch.elapsedTime();
return timeToSelectionSort;
}
Run Code Online (Sandbox Code Playgroud)
问题是我需要为我的所有排序算法创建这个方法(插入,选择,快速排序,合并...)
有没有办法将这些算法作为此方法的参数传递?
是的,一点没错.这就是所谓的战略模式.基本上,创建一个接口,让每个算法都是一个实现接口的类,并使参数具有参数类型(我在这里使用C#约定)
public interface SortingAlgo {
void sort(...);
}
public class QuickSort implements SortingAlgo {
public void sort(...) {
...
}
}
public void methodYouWantToAcceptAlgo(SortingAlgo algo) {
...
}
Run Code Online (Sandbox Code Playgroud)
等等.