Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gna;
- import java.io.File;
- import java.util.Arrays;
- import java.util.Collections;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.ChartUtilities;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.plot.XYPlot;
- import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
- import org.jfree.data.xy.XYSeries;
- import org.jfree.data.xy.XYSeriesCollection;
- import org.jfree.ui.ApplicationFrame;
- public class SortingAlgorithmsCharts {
- public static void main(String[] args) {
- XYSeriesCollection dataset;
- SortingAlgorithms algorithms = new SortingAlgorithms();
- new SortingAlgorithmsCharts();
- dataset = new XYSeriesCollection();
- XYSeries selectionSort = new XYSeries("Selection Sort");
- XYSeries insertionSort = new XYSeries("Insertion Sort");
- XYSeries mergeSort = new XYSeries("Merge Sort");
- XYSeries quickSort = new XYSeries("Quick Sort");
- for (int j = 0; j <= 99; j++) {
- for (int k = 1; k <= 100; k++) {
- Integer[] array = new Integer[k];
- for (int n = 0; n < k; n++) {
- array[n] = n;
- }
- Collections.shuffle(Arrays.asList(array));
- Integer[] aux = array.clone();
- selectionSort.add(k, algorithms.selectionSort(array));
- array = aux.clone();
- insertionSort.add(k, algorithms.insertionSort(array));
- array = aux.clone();
- mergeSort.add(k, algorithms.mergeSort(array));
- array = aux.clone();
- quickSort.add(k, algorithms.quickSort(array));
- }
- }
- dataset.addSeries(selectionSort);
- dataset.addSeries(insertionSort);
- dataset.addSeries(mergeSort);
- dataset.addSeries(quickSort);
- final JFreeChart chart = ChartFactory.createScatterPlot(
- "Shuffled array", // chart title
- "Number of elements", // x axis label
- "Number of comparisons", // y axis label
- dataset, // data
- PlotOrientation.VERTICAL, true, // include legend
- true, // tooltips
- false // urls
- );
- XYPlot plot = (XYPlot) chart.getPlot();
- XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
- renderer.setSeriesLinesVisible(0, true);
- plot.setRenderer(renderer);
- final ChartPanel chartPanel = new ChartPanel(chart);
- chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
- final ApplicationFrame frame = new ApplicationFrame("Title");
- frame.setContentPane(chartPanel);
- frame.pack();
- frame.setVisible(true);
- try {
- ChartUtilities
- .saveChartAsJPEG(new File(
- "D:\\Schoolwerk\\G&A\\shuffledChart.jpg"), chart,
- 500, 300);
- System.out.println("Chart created succesfully.");
- } catch (Exception e) {
- System.out.println("Problem occurred creating chart.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement