社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
所有排序算法源码地址: https://github.com/WeCanRun/Sorting-Algorithm
【排序算法(一)】选择排序及其改进
【排序算法(二)】冒泡排序及其改进
【排序算法(三)】直接插入排序及其改进
【排序算法(四)】希尔排序
【排序算法(五)】归并排序
【排序算法(六)】快速排序
【排序算法(七)】基数排序
【数据结构】堆应用之堆排序
【排序算法(八)】排序算法总结(上)
package com.wuyi.notecode.sort;
import java.util.Random;
public class Main<T extends Comparable<T>> {
public long testSort(Sort<Integer> sort) {
Integer[] nums = new Integer[10000];
Random random = new Random();
for (int i = 0; i < 10000; i++) {
nums[i] = random.nextInt();
}
long start = System.currentTimeMillis();
sort.sort(nums);
long end = System.currentTimeMillis();
return end - start;
}
public static void main(String[] args) {
Main<Integer> main = new Main<>();
BubbleSort1<Integer> bubbleSort1 = new BubbleSort1<>();
long res = main.testSort(bubbleSort1);
System.out.println("BubbleSort1 运行时间:" + res + "毫秒");
BubbleSort2<Integer> bubbleSort2 = new BubbleSort2<>();
res = main.testSort(bubbleSort2);
System.out.println("BubbleSort2 运行时间:" + res + "毫秒");
SelectionSort1<Integer> selectionSort1 = new SelectionSort1<>();
res = main.testSort(selectionSort1);
System.out.println("SelectionSort1 运行时间:" + res + "毫秒");
SelectionSort2<Integer> selectionSort2 = new SelectionSort2<>();
res = main.testSort(selectionSort2);
System.out.println("SelectionSort2 运行时间:" + res + "毫秒");
InsertionSort1<Integer> insertionSort1 = new InsertionSort1<>();
res = main.testSort(insertionSort1);
System.out.println("InsertionSort1 运行时间:" + res + "毫秒");
InsertionSort2<Integer> insertionSort2 = new InsertionSort2<>();
res = main.testSort(insertionSort2);
System.out.println("InsertionSort2 运行时间:" + res + "毫秒");
ShellSort<Integer> shellSort = new ShellSort<>();
res = main.testSort(shellSort);
System.out.println("ShellSort 运行时间:" + res + "毫秒");
HeapSort<Integer> heapSort = new HeapSort<>();
res = main.testSort(heapSort);
System.out.println("HeapSort 运行时间:" + res + "毫秒");
MergeSort<Integer> mergeSort = new MergeSort<>();
res = main.testSort(mergeSort);
System.out.println("MergeSort 运行时间:" + res + "毫秒");
QuickSort<Integer> quickSort = new QuickSort<>();
res = main.testSort(quickSort);
System.out.println("QuickSort 运行时间:" + res + "毫秒");
ThreeWayQuickSort<Integer> threeWayQuickSort = new ThreeWayQuickSort<>();
res = main.testSort(threeWayQuickSort);
System.out.println("ThreeWayQuickSort 运行时间:" + res + "毫秒");
}
}
快速排序是最快的通用排序算法,它的内循环的指令很少,而且它还能利用缓存,因为它总是顺序地访问数据。它的运行时间近似为~cNlogN,这里的 c 比其它线性对数级别的排序算法都要小。
使用三向切分快速排序,实际应用中可能出现的某些分布的输入能够达到线性级别,而其它排序算法仍然需要线性对数时间。
Java 主要排序方法为 java.util.Arrays.sort (),对于原始数据类型使用三向切分的快速排序,对于引用类型使用归并排序。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!