选择、插入、冒泡、计数、堆、归并、快速排序算法的Java代码汇总和GUI界面时间测试与讲解

运行效果:
在这里插入图片描述

Video_2024-04-16_150519


前言:排序算法在数据结构和算法中的重要性

排序算法在数据结构和算法中占据着极其重要的地位。它们不仅是计算机科学中的基础概念,还是解决许多实际问题的关键工具。以下是排序算法在数据结构和算法中的重要性的一些体现:

数据处理的基础:排序是数据处理中的基本操作之一。在许多应用中,我们需要对一组数据进行排序,以便进一步的分析或操作。例如,在数据库中,经常需要对查询结果进行排序以满足用户的需求。

算法设计和分析的基础:学习排序算法可以帮助我们理解算法设计的基本原则,如时间复杂度、空间复杂度、稳定性等。此外,排序算法还提供了丰富的实例,用于学习算法分析和优化的技巧。

解决实际问题的工具:排序算法在解决实际问题中发挥着重要作用。例如,在搜索引擎中,排序算法用于确定搜索结果的顺序;在推荐系统中,排序算法用于确定推荐的优先级;在机器学习中,排序算法用于处理有序数据或生成有序的输出。

优化和效率的关键:不同的排序算法具有不同的时间复杂度和空间复杂度。选择合适的排序算法对于提高程序的运行效率至关重要。例如,在处理大规模数据时,我们需要选择时间复杂度较低的排序算法,如快速排序、归并排序等。

培养逻辑思维和问题解决能力:学习和应用排序算法可以培养我们的逻辑思维和问题解决能力。通过分析问题、设计算法、实现算法并测试其性能,我们可以逐步提高自己的编程能力和算法设计能力。

总之,排序算法在数据结构和算法中的重要性不容忽视。它们不仅是计算机科学的基础,还是解决实际问题的关键工具。通过学习和掌握排序算法,我们可以更好地理解和应用数据结构和算法的其他概念和技术。

作者应用Java GUI制作了一个可以简单测量各种排序算法运行时间的小程序,其中涵盖了对各个排序算法的简单讲解并在这推荐一个排序可视化的神仙链接: Comparison Sorting Algorithms方便对排序算法的理解
在这里插入图片描述


一、排序算法详解

1.选择排序

时间复杂度:
平均时间复杂度: O(n^2)
最坏时间复杂度: O(n^2)
最好时间复杂度: O(n^2)
空间复杂度: O(1)

稳定性: 不稳定

解释:
选择排序的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。选择排序是不稳定的,因为交换可能破坏相等元素的原始顺序。

/*
    选择排序:
     */
    public void SelectionSort(int array[])
    {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
        }


    }

2.插入排序

时间复杂度:

平均时间复杂度: O(n^2)
最坏时间复杂度: O(n^2)
最好时间复杂度: O(n)
空间复杂度: O(1)

稳定性: 稳定

解释:
插入排序的工作方式是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

/*
    插入排序:
     */
    public void InsertSort(int array[])
    {
        int curIndex;
        for (int i = 0; i < array.length - 1; i++) {
            int preIndex = i;
            curIndex =array[preIndex + 1];

            while(preIndex >= 0 && curIndex < array[preIndex]) {
                array[preIndex + 1] = array[preIndex];
                preIndex--;
            }
            array[preIndex + 1] = curIndex;
        }


    }

3.冒泡排序

时间复杂度:

平均时间复杂度: O(n^2)
最坏时间复杂度: O(n^2)
最好时间复杂度: O(n)
空间复杂度: O(1)

稳定性: 稳定

解释:
冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

/*
    冒泡排序

     */
    public void BubbleSort(int array[])
    {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] < array[j]) {
                    int temp = array[j + 1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }


    }

4.计数排序

时间复杂度:

平均时间复杂度: O(n + k)
最坏时间复杂度: O(n + k)
最好时间复杂度: O(n + k)
空间复杂度: O(k)

稳定性: 稳定

解释:
计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。计数排序不是比较排序,排序的速度快于任何比较排序算法。由于用来计数的数组C的长度取决于待排序数组中数据的范围(等于待排序数组的最大值与最小值的差加1),这使得计数排序对于数据范围很大的数组,需要大量时间和内存。

/*
    计数排序
     */
    public void CountingSort(int array[])
    {
        int gap,min = array[0],max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
            if (array[i] < min) {
                min = array[i];
            }
        }

        gap = 0 - min;
        int[] counter = new int[max - min +1 ];
        Arrays.fill(counter, 0);

        for (int i = 0; i < array.length; i++) {
            counter[array[i] + gap]++;
        }

        int index = 0;
        int i = 0;

        while(index < array.length) {
            if (counter[i] != 0 ) {
                array[index] = i -gap;
                counter[i] --;
                index++;
            }else {
                i++;
            }
        }


    }

5.堆排序

时间复杂度:

平均时间复杂度: O(nlogn)
最坏时间复杂度: O(nlogn)
最好时间复杂度: O(nlogn)
空间复杂度: O(1)

稳定性: 不稳定

解释:
堆排序是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子节点的键值或索引总是小于(或者大于)它的父节点。堆排序可以说是一种利用堆这种数据结构所设计的一种排序算法,它是选择排序的一种。可以利用数组来模拟堆的结构以节省空间。堆排序是一种不稳定的排序方法。

/*
    堆排序:
     */
    int n;
    public void HeapSort(int array[]) {
        n = array.length;
        buildMaxHeap(array);
        while(n > 0) {
            swap1(array, 0,n - 1);
            n--;
            adjustHeap(array, 0);
        }
    }

    public void buildMaxHeap(int[] nums) {
        for (int i = (n/2 - 1); i >= 0; i--) {
            adjustHeap(nums,i);
        }
    }

    public void adjustHeap(int[] nums,int i) {
        int maxIndex = i;
        int left = 2*i + 1;
        int right = 2*(i + 1);

        if (left < n && nums[left] > nums[maxIndex]) {
            maxIndex = left;
        }

        if (right < n && nums[right] > nums[maxIndex] && nums[right] > nums[left]) {
            maxIndex = right;
        }

        if (maxIndex != i) {
            swap(nums, maxIndex, i);
            adjustHeap(nums, maxIndex);
        }

    }

    public void swap1(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

6.归并排序

时间复杂度:

平均时间复杂度: O(nlogn)
最坏时间复杂度: O(nlogn)
最好时间复杂度: O(nlogn)
空间复杂度: O(n)

稳定性: 稳定

解释:
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。归并排序是一种稳定的排序方法。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。

/*
    归并排序
     */
    public void mergesort(int[] nums) {
        int[] nums1 = MergeSort(nums);
        for (int i = 0; i < nums1.length; i++) {
            nums[i] = nums1[i];
        }
    }
    public int[] MergeSort(int[] nums) {
        int n = nums.length;
        int mid = n / 2;
        if (n < 2) {
            return nums;
        }
        int[] left = Arrays.copyOfRange(nums, 0, mid);
        int[] right = Arrays.copyOfRange(nums, mid, n);
        return merge(MergeSort(left), MergeSort(right));

    }

    public int[] merge(int[] left, int[] right) {
        int[] res = new int[left.length + right.length];
        for (int i = 0, index = 0, j = 0; index < res.length; index++) {
            if (i >= left.length) {
                res[index] = right[j++];
            } else if (j >= right.length) {
                res[index] = left[i++];
            } else if (left[i] > right[j]) {
                res[index] = right[j++];
            } else {
                res[index] = left[i++];
            }
        }
        return res;
    }

7.快速排序

快速排序是一种高效的排序算法,它采用了分治法的思想。基本步骤是选择一个基准元素,通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

时间复杂度:

平均时间复杂度:O(nlogn)
最坏时间复杂度:O(n^2)
最好时间复杂度:O(nlogn)
在平均情况下,快速排序的时间复杂度为O(nlogn),这是非常高效的。然而,在最坏情况下,当输入数据已经有序或接近有序时,快速排序的时间复杂度会退化为O(n^2),性能较差。为了避免最坏情况的发生,可以采用随机化快速排序,即在每次划分前随机选择一个基准元素,这样可以降低最坏情况发生的概率。

空间复杂度:

快速排序的空间复杂度主要取决于递归调用的深度。在最好情况下,递归树的深度为O(logn),因此空间复杂度为O(logn)。然而,在最坏情况下,当输入数据有序或接近有序时,递归树的深度可能达到O(n),导致空间复杂度也为O(n)。同样,通过采用尾递归优化或迭代实现,可以降低空间复杂度。

稳定性:

快速排序是不稳定的排序算法。因为在划分过程中,相等的元素可能会因为基准元素的选取和交换操作而改变它们的相对顺序。所以,如果对于排序的稳定性有要求,快速排序可能不是最佳选择。

    /*
    快速排序
     */

    public void QuickSort(int array[])
    {
       int s=array.length;
        sort(array,0,s-1);

    }
    public void sort(int[] nums, int start, int end) {
        int zoneIndex = partition(nums, start, end);

        if (zoneIndex > start) {
            sort(nums, start, zoneIndex - 1);
        }

        if (zoneIndex < end) {
            sort(nums, zoneIndex + 1, end);
        }
    }

    public int partition(int[] nums, int start, int end) {
        int pivot = (int) (start + Math.random() * (end - start + 1));
        int zoneIndex = start - 1;
        swap(nums, pivot, end);
        for (int i = start; i <= end; i++) {
            if (nums[i] <= nums[end]) {
                zoneIndex++;

                if (i > zoneIndex) {
                    swap(nums, i, zoneIndex);
                }

            }

        }
        return zoneIndex;

    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

二、实现一个可以计算时间的Java GUI排序应用程序

结构:
在这里插入图片描述
Compare:

import javax.swing.*;
import java.awt.*;

public class Compare extends JFrame {
    JPanel jPanel=new JPanel();
    public Compare(){
        this.setTitle("算法比较");
        this.setSize(1024,680);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/compare.png").getImage();
        g.drawImage(image,20,40,800,600,null);
    }
}

Introduce1

import javax.swing.*;
import java.awt.*;

public class Introduce1 extends JFrame {
    JPanel jPanel=new JPanel();
    public Introduce1(){
        this.setSize(800,660);
        this.setTitle("选择排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/selectsort.png").getImage();
        g.drawImage(image,0,20,400,500,null);
        Image image1=new ImageIcon("image/select1.png").getImage();
        g.drawImage(image1,400,20,400,200,null);
    }
}

Introduce2

import javax.swing.*;
import java.awt.*;

public class Introduce2 extends JFrame {
    JPanel jPanel=new JPanel();
    public Introduce2(){
        this.setSize(800,660);
        this.setTitle("插入排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/insert.png").getImage();
        g.drawImage(image,0,20,400,500,null);
        Image image1=new ImageIcon("image/insert1.png").getImage();
        g.drawImage(image1,400,20,400,200,null);
    }
}

Introduce3

import javax.swing.*;
import java.awt.*;

public class Introduce3 extends JFrame{
    JPanel jPanel=new JPanel();
    public Introduce3(){
        this.setSize(800,660);
        this.setTitle("冒泡排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/bubble.png").getImage();
        g.drawImage(image,0,30,400,500,null);
        Image image1=new ImageIcon("image/bubble1.png").getImage();
        g.drawImage(image1,400,30,400,200,null);
    }
}

Introduce4

import javax.swing.*;
import java.awt.*;

public class Introduce4 extends JFrame {
    JPanel jPanel=new JPanel();
    public Introduce4(){
        this.setSize(910,680);
        this.setTitle("计数排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
   jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/counting.png").getImage();
        g.drawImage(image,80,30,700,400,null);
     Image image1=new ImageIcon("image/count1.png").getImage();
      g.drawImage(image1,0,430,800,280,null);
    }
}

Introduce5

import javax.swing.*;
import java.awt.*;

public class Introduce5 extends JFrame{
    JPanel jPanel=new JPanel();
    public Introduce5(){
        this.setSize(1000,660);
        this.setTitle("堆排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/heap.png").getImage();
        g.drawImage(image,0,30,500,600,null);
        Image image1=new ImageIcon("image/heap1.png").getImage();
        g.drawImage(image1,500,30,400,300,null);
    }
}

Introduce6

import javax.swing.*;
import java.awt.*;

public class Introduce6 extends JFrame {
    JPanel jPanel=new JPanel();
    public Introduce6(){
        this.setSize(1000,660);
        this.setTitle("归并排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/merge.png").getImage();
        g.drawImage(image,0,20,500,500,null);
        Image image1=new ImageIcon("image/merge1.png").getImage();
        g.drawImage(image1,500,20,400,300,null);
    }
}

Introduce7

import javax.swing.*;
import java.awt.*;

public class Introduce7 extends JFrame {
    JPanel jPanel=new JPanel();
    public Introduce7(){
        this.setSize(1000,680);
        this.setTitle("快速排序简介");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.add(jPanel);
        jPanel.setBackground(Color.white);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Image image=new ImageIcon("image/quick.png").getImage();
        g.drawImage(image,0,20,400,600,null);
        Image image1=new ImageIcon("image/quicksort.png").getImage();
        g.drawImage(image1,400,20,400,600,null);
    }
}

sort

import java.util.Arrays;

public class Sort {

    /*
    插入排序:
     */
    public void InsertSort(int array[])
    {
        int curIndex;
        for (int i = 0; i < array.length - 1; i++) {
            int preIndex = i;
            curIndex =array[preIndex + 1];

            while(preIndex >= 0 && curIndex < array[preIndex]) {
                array[preIndex + 1] = array[preIndex];
                preIndex--;
            }
            array[preIndex + 1] = curIndex;
        }


    }
    /*
    归并排序
     */
    public void mergesort(int[] nums) {
        int[] nums1 = MergeSort(nums);
        for (int i = 0; i < nums1.length; i++) {
            nums[i] = nums1[i];
        }
    }
    public int[] MergeSort(int[] nums) {
        int n = nums.length;
        int mid = n / 2;
        if (n < 2) {
            return nums;
        }
        int[] left = Arrays.copyOfRange(nums, 0, mid);
        int[] right = Arrays.copyOfRange(nums, mid, n);
        return merge(MergeSort(left), MergeSort(right));

    }

    public int[] merge(int[] left, int[] right) {
        int[] res = new int[left.length + right.length];
        for (int i = 0, index = 0, j = 0; index < res.length; index++) {
            if (i >= left.length) {
                res[index] = right[j++];
            } else if (j >= right.length) {
                res[index] = left[i++];
            } else if (left[i] > right[j]) {
                res[index] = right[j++];
            } else {
                res[index] = left[i++];
            }
        }
        return res;
    }


    /*
    快速排序
     */

    public void QuickSort(int array[])
    {
       int s=array.length;
        sort(array,0,s-1);

    }
    public void sort(int[] nums, int start, int end) {
        int zoneIndex = partition(nums, start, end);

        if (zoneIndex > start) {
            sort(nums, start, zoneIndex - 1);
        }

        if (zoneIndex < end) {
            sort(nums, zoneIndex + 1, end);
        }
    }

    public int partition(int[] nums, int start, int end) {
        int pivot = (int) (start + Math.random() * (end - start + 1));
        int zoneIndex = start - 1;
        swap(nums, pivot, end);
        for (int i = start; i <= end; i++) {
            if (nums[i] <= nums[end]) {
                zoneIndex++;

                if (i > zoneIndex) {
                    swap(nums, i, zoneIndex);
                }

            }

        }
        return zoneIndex;

    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    /*
    堆排序:
     */
    int n;
    public void HeapSort(int array[]) {
        n = array.length;
        buildMaxHeap(array);
        while(n > 0) {
            swap1(array, 0,n - 1);
            n--;
            adjustHeap(array, 0);
        }
    }

    public void buildMaxHeap(int[] nums) {
        for (int i = (n/2 - 1); i >= 0; i--) {
            adjustHeap(nums,i);
        }
    }

    public void adjustHeap(int[] nums,int i) {
        int maxIndex = i;
        int left = 2*i + 1;
        int right = 2*(i + 1);

        if (left < n && nums[left] > nums[maxIndex]) {
            maxIndex = left;
        }

        if (right < n && nums[right] > nums[maxIndex] && nums[right] > nums[left]) {
            maxIndex = right;
        }

        if (maxIndex != i) {
            swap(nums, maxIndex, i);
            adjustHeap(nums, maxIndex);
        }

    }

    public void swap1(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    /*
    冒泡排序

     */
    public void BubbleSort(int array[])
    {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] < array[j]) {
                    int temp = array[j + 1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }


    }
    /*
    选择排序:
     */
    public void SelectionSort(int array[])
    {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
        }


    }
    /*
    计数排序
     */
    public void CountingSort(int array[])
    {
        int gap,min = array[0],max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
            if (array[i] < min) {
                min = array[i];
            }
        }

        gap = 0 - min;
        int[] counter = new int[max - min +1 ];
        Arrays.fill(counter, 0);

        for (int i = 0; i < array.length; i++) {
            counter[array[i] + gap]++;
        }

        int index = 0;
        int i = 0;

        while(index < array.length) {
            if (counter[i] != 0 ) {
                array[index] = i -gap;
                counter[i] --;
                index++;
            }else {
                i++;
            }
        }


    }
}

SortPanel:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class SortPanel extends JPanel implements ActionListener {
    int state=0;
    JTextField jTextField=new JTextField(40);
    JTextArea area1=new JTextArea();
    JTextArea area2=new JTextArea();
    JTextArea area3=new JTextArea();
    JTextArea area4=new JTextArea();
    JTextArea area5=new JTextArea();
    JTextArea area6=new JTextArea();
    JTextArea area7=new JTextArea();

    JButton btn0=new JButton("清空");
    JButton btn1=new JButton("选择排序");
    JButton btn2=new JButton("插入排序");
    JButton btn3=new JButton("冒泡排序");
    JButton btn4=new JButton("计数排序");
    JButton btn5=new JButton("堆排序");
    JButton btn6=new JButton("归并排序");
    JButton btn7=new JButton("快速排序");
    JButton btn8=new JButton("生成随机数");
    long time;
    long naSeconds;


    public SortPanel()
    {
        this.setLayout(null);
        jTextField.setLocation(380,15);
        jTextField.setSize(200,25);

        btn0.setBounds(new Rectangle(585,11,70,30));
        btn8.setBounds(new Rectangle(660,11,100,30));

        btn1.setBounds(new Rectangle(135-50,201,90,50));
        btn2.setBounds(new Rectangle(391-50,201,90,50));
        btn3.setBounds(new Rectangle(647-50,201,90,50));
        btn4.setBounds(new Rectangle(903-50,201,90,50));
        btn5.setBounds(new Rectangle(135-50,401,90,50));
        btn6.setBounds(new Rectangle(391-50,401,90,50));
        btn7.setBounds(new Rectangle(647-50,401,90,50));

        this.add(jTextField);
        this.add(btn0);
        this.add(btn1);
        this.add(btn2);
        this.add(btn3);
        this.add(btn4);
        this.add(btn5);
        this.add(btn6);
        this.add(btn7);
        this.add(btn8);

        btn0.addActionListener(this);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);

        btn8.addActionListener(this);







        JScrollPane sp1=new JScrollPane(area1);
        JScrollPane sp2=new JScrollPane(area2);
        JScrollPane sp3=new JScrollPane(area3);
        JScrollPane sp4=new JScrollPane(area4);
        JScrollPane sp5=new JScrollPane(area5);
        JScrollPane sp6=new JScrollPane(area6);
        JScrollPane sp7=new JScrollPane(area7);



        sp1.setLocation(30,261);
        sp2.setLocation(280,261);
        sp3.setLocation(530,261);
        sp4.setLocation(780,261);
        sp5.setLocation(30,455);
        sp6.setLocation(280,455);
        sp7.setLocation(530,455);

        sp1.setSize(200,100);
        sp2.setSize(200,100);
        sp3.setSize(200,100);
        sp4.setSize(200,100);
        sp5.setSize(200,100);
        sp6.setSize(200,100);
        sp7.setSize(200,100);



        this.add(sp1);
        this.add(sp2);
        this.add(sp3);
        this.add(sp4);
        this.add(sp5);
        this.add(sp6);
        this.add(sp7);








    }


    public void actionPerformed(ActionEvent e) {
        JButton btn=(JButton)e.getSource();
        if(btn==btn0)
        {

            jTextField.setText(null);
            area1.setText(null);
            area2.setText(null);
            area3.setText(null);
            area4.setText(null);
            area5.setText(null);
            area6.setText(null);
            area7.setText(null);

        }
        else
        {
            String str=jTextField.getText();
            if(btn==btn8)
            {
                int n=Integer.valueOf(str);
                int []ran=new int[n];
                String primary="";
                for(int i=0;i<n;i++)
                {
                    int random=(int)(Math.random()*10000);
                    ran[i]=random;
                    primary=primary+random+" ";
                }
                jTextField.setText(primary);



            }
            else {
                String[] split = str.split(" ");
                int j=split.length;
                int array[]=new int[j];
                for (int i = 0; i < j; i++) {
                    array[i]=Integer.valueOf(split[i]);


                }
                if(btn==btn1) {

                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().InsertSort(array);
                    long end1 = System.currentTimeMillis();
                    long secondend=System.nanoTime();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area1.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");
                }
                if(btn==btn2) {

                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().SelectionSort(array);
                    long end1 = System.currentTimeMillis();
                    long secondend=System.nanoTime();
                    naSeconds=secondend-secondstart;
                    time=end1-start1;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area2.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");
                }
                if(btn==btn3) {

                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().SelectionSort(array);
                    long secondend=System.nanoTime();
                    long end1 = System.currentTimeMillis();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area3.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");
                }
                if(btn==btn4) {

                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().HeapSort(array);
                    long secondend=System.nanoTime();
                    long end1 = System.currentTimeMillis();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area4.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");
                }
                if(btn==btn5)
                {
                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().CountingSort(array);
                    long secondend=System.nanoTime();

                    long end1 = System.currentTimeMillis();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area5.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");

                }
                if(btn==btn6)
                {
                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().mergesort(array);
                    long secondend=System.nanoTime();
                    long end1 = System.currentTimeMillis();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area6.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");

                }
                if(btn==btn7)
                {
                    long start1 = System.currentTimeMillis();
                    long secondstart=System.nanoTime();
                    new Sort().QuickSort(array);
                    long secondend=System.nanoTime();
                    long end1 = System.currentTimeMillis();
                    time=end1-start1;
                    naSeconds=secondend-secondstart;


                    String s = "排序后:";
                    for (int i = 0; i < j; i++) {
                        s = s + String.valueOf(array[i]) + " ";

                    }
                    area7.setText(s+"\r\n"+"用时----"+time+"毫秒"
                            +"\n\r"+"用时----"+naSeconds+"纳秒");

                }

            }

        }

    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
//        if (state == 1)
//        {
//            g.setColor(Color.GREEN);
//        Font f = new Font("黑体", Font.BOLD, 10);
//        g.setFont(f);
//        g.drawString("用时" + time, 20, 20);
//    }



    }
}

UI:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UI extends JFrame{
    JMenuBar mb=new JMenuBar();
    JMenu m1=new JMenu("排序简介");
    JMenuItem item0=new JMenuItem("排序比较");
    JMenuItem item1=new JMenuItem("选择排序");
    JMenuItem item2=new JMenuItem("插入排序");
    JMenuItem item3=new JMenuItem("冒泡排序");
    JMenuItem item4=new JMenuItem("计数排序");
    JMenuItem item5=new JMenuItem("堆排序");
    JMenuItem item6=new JMenuItem("归并排序");
    JMenuItem item7=new JMenuItem("快速排序");

    SortPanel sortPanel=new SortPanel();


    public UI()
    {
        mb.add(m1);
        m1.add(item0);
        item0.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Compare();
            }
        });
        m1.add(item1);
        item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce1();
            }
        });
        m1.add(item2);
        item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce2();
            }
        });
        m1.add(item3);
        m1.add(item4);
        m1.add(item5);
        m1.add(item6);
        m1.add(item7);
        item3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce3();
            }
        });
        item4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce4();
            }
        });
        item5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce5();
            }
        });
        item6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce6();
            }
        });
        item7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Introduce7();
            }
        });



        this.add(sortPanel);
        this.setJMenuBar(mb);



        this.setTitle("算法");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1024,680);
    }

    public static void main(String[] args) {
        new UI();
    }
}

源码和图片资源放入到下方的网盘链接里了
链接:https://pan.baidu.com/s/1DvyReYmrWh7BGdwJze8iGQ?pwd=2zjh
提取码:2zjh
链接: 百度网盘排序源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值