快速排序、堆排序和归并排序都是平均时间复杂度为O(nlog(n))的算法。有关其原理介绍已经有很多。今天写了个简单的实现,用JFreeChart做了个简单的性能比较。懒得保存在本地,上传一下,以供日后回看。
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int time = 400;
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0; i<time; i++) {
int len = (i+1)*800;
int scale = len*2;
int[] arr = new int[len];
for (int j=0; j<len; j++) {
arr[j] = (int)Math.floor(Math.random()*scale);
}
Calendar begin = Calendar.getInstance();
quickSort(arr, 0, len-1);
Calendar q = Calendar.getInstance();
dataset.addValue(q.getTimeI