Home > Java > javaTutorial > body text

Common sorting algorithms for Java data structures (summary sharing)

WBOY
Release: 2023-01-22 06:30:01
forward
1743 people have browsed it

This article brings you relevant knowledge about Java, which mainly introduces some common sorting algorithms, including direct insertion sorting, Hill sorting (reducing incremental sorting), selection sorting, and heap sorting, etc. , let’s take a look at it, I hope it will be helpful to everyone.

Common sorting algorithms for Java data structures (summary sharing)

1. Understanding order

In school, if we want to participate in sports meets or military training, we will Line up according to height from short to tall. For example, the attendance sheet held by the teacher in class is usually sorted according to student number from low to high. Another example is the programming language rankings, which are also sorted.

There are quite a lot of sorting scenarios in life. It can be seen that sorting is still very important. This chapter will introduce some common sorting algorithms.

The so-called sorting, take our example above, is the operation of arranging incrementing or decrementing according to the size of one or some keywords. This is sorting, which also involves Stability of sorting, for example:

For example, there is a set of data: B D A C A F. It needs to be sorted according to their ascll codes. There are two A, we call the first A that appears A1, and the second A that appears A2.

Assuming that the result after sorting is: A1 A2 B C D F, then this sorting algorithm is stable.

Assume that the result after sorting is: A2 A1 B C D F, then this sorting algorithm is unstable.

In short, if there are two identical elements in the data to be sorted, the relationship between the two elements will not change after the sorting is completed. For example, A1 is in front of A2 before sorting. After sorting, A1 is still in front of A2. This is a stable sorting algorithm.

Note: An unstable sorting algorithm is inherently unstable, but a stable sorting algorithm can be designed to be unstable.

2. Classification of common sorting

#This picture summarizes the sorting algorithm we will talk about later, and then officially enter the study of this chapter! (In the sorting algorithm chapter, the default is Ascending order) Note: The complexity log mentioned later is based on 2, and special ones will be marked.

3. Direct insertion sorting

Now I would like to ask all my friends to imagine that they are touching playing cards. They touched the first card and put it in their hands, and then Draw one card, compare this card with a card in your hand, and put it in the appropriate position. Then draw another card, compare this card with the two cards in your hand, and put it in the appropriate position. Location.

This is direct insertion sorting. To put it simply, the elements we take every time will be inserted into an ordered sequence. That is, before each card is drawn, the cards in hand are sorted. Yes, we only need to compare the newly drawn cards with the ordered cards in our hand and put them in the appropriate position!

Here we use a static picture to briefly demonstrate:

We already understand the general idea, next We need to use code to implement it:

public void insertSort(int[] array) {
    // 外循环控制趟数, 第一张牌默认有序, 所以 i 从 1 开始
    for (int i = 1; i < array.length; i++) {
        int tmp = array[i]; //当前摸到的牌
        // 每次从手中牌的最后一张牌开始比较, 一直比到第一张牌
        int j = i - 1;
        for (; j >= 0; j--) {
            //如果当前位置的牌,大于我摸到的牌,就往后挪
            if (array[j] > tmp) {
                array[j + 1] = array[j];
            } else {
                break;
            }
        }
        // 把摸到的牌放到对应位置上
        array[j + 1] = tmp;
    }
}
Copy after login
  • Time complexity analysis: The outer loop takes n - 1 times in total, and the inner loop is the worst case each time Next, we need to compare 1...n times, then remove the small term before n, that is, (n - 1) * n times, that is, n^2 - n, remove the minimum term, and the final time complexity is O(n^2)
  • Space complexity analysis:Just open up a tmp variable i, j, constant, that is, space complexity: O(1)
  • Stability: Stable
  • The closer the data is to the ordered situation, the higher the time efficiency.

4. Hill sorting (reducing incremental sorting)

This sorting is an optimization of direct insertion sorting. You can imagine that there are side-by-side rows in front of you. Okay, 8 love number plates, but they are out of order. We need to group the number plates. According to the requirements, the first interval is 4 number plates as a group. After the groups are divided, direct insertion sorting is performed. The second time The second interval is a group of 2 number plates, and the direct insertion sort is performed. The third interval is a group of 1 number plate, and the third interval is a group of number plates, and the direct insertion sort is performed.

I don’t understand this a little bit, it doesn’t matter, let’s draw a picture to understand what I said above again:

由上图我们可以发现,当间隔 > 1 的时候,都是预排序,也就是让我们的数据更接近有序,但是当间隔为 1 的时候,就是直接插入排序了,前面我们说过,直接插入排序,再数据接近有序的时候时间效率是很快的。由此可见,希尔排序,是直接插入排序的优化版。

如何在代码中实现呢?间隔的值如何取呢?代码中把这个间隔的值称为 gap,这个 gap 的取值方法有很多,有的人提出 gap 为奇数好,有的提出 gap 为偶数好,我们就采取一种比较简单的方法来取 gap 值,首次取数组长度一半的值为 gap,后续 gap /= 2,即可。当 gap 为 1,也就是直接插入排序了。

代码实现如下:

public void shellSort(int[] array) {
    // gap初始值设置成数组长度的一半
    int gap = array.length >> 1;
    // gap 为 1 的时候直接插入排序
    while (gap >= 1) {
        shell(array, gap);
        gap >>= 1; // 更新 gap 值 等价于 -> gap /= 2;
    }
}
private void shell(int[] array, int gap) {
    for (int i = gap; i < array.length; i++) {
        int tmp = array[i];
        int j = i - gap;
        for (; j >= 0; j -= gap) {
            if (array[j] > tmp) {
                array[j + gap] = array[j];
            } else {
                break;
            }
        }
        array[j + gap] = tmp;
    }
}
Copy after login

如果实在是不好理解,就结合上边讲的直接插入排序来理解,相信你能理解到的。

  • 时间复杂度分析:希尔排序的时间复杂度不好分析, 这里我们就大概记一下,约为 O(n^1.3),感兴趣的话,可以查阅一下相关书籍。
  • 空间复杂度分析:仍然开辟的是常数个变量,空间复杂度为 O(1)
  • 稳定性:不稳定

5、选择排序

这个排序是个很简单的排序,你想象一下,有个小屁孩,喜欢玩小球,我给他安排了个任务,把这一排小球从小到大排列起来,摆给我看,于是小屁孩就找,每次从一排小球中找出最大的,放到最后,固定不动,那是不是也就是说,每次能确定一个最大的石子的最终位置了。我们来看图:

通过图片我们也能看出来,每次找到最大值于最后一个值交换,所以每趟都能把最大的放到最后固定不动,每趟能排序一个元素出来,那这样用代码来实现就很简单了:

public void selectSort(int[] array) {
    int end = array.length - 1;
    // 剩最后一个元素的时候, 不用比较了, 已经有序了
    // 所以 i < array.length - 1
    for (int i = 0; i < array.length - 1; i++) {
        int max = 0;
        int j = 0;
        while (j <= end) {
            if (array[j] > array[max]) {
                max = j;
            }
            j++;
        }
        //找到了最大值的下标, 把最大值与最后一个值交换
        swap(array, max, end--); // end-- 最后一个元素固定了, 不用参与比较
    }
}
Copy after login

这个算法有没有可以优化的空间呢?

有!那么既然小屁孩能一次找出最大的球,那能不能让小屁孩一次找出两个球出来呢?分别是这些球中,最大的和最小的,最大的放在最右边,最小的放在最左边,那么我们每次就能确定两个球的最终位置,也就是我们一次能排序两个元素。图解:

代码实现如下:

public void selectSort(int[] array) {
    int left = 0;
    int right = array.length - 1;
    while (left < right) {
        int maxIndex = left;
        int minIndex = left;
        // i = left + 1 -> 每次找最大最小值下标的时候, 可以不用算默认给的最大值和最小值下标
        for (int i = left + 1; i <= right; i++) {
            if (array[i] > array[maxIndex]) {
                maxIndex = i;
            }
            if (array[i] < array[minIndex]) {
                minIndex = i;
            }
        }
        swap(array, minIndex, left);
        // 如果最大值为 left 的位置情况的话, 走到这, 最大值已经被交换到 min 位置上了
        if (maxIndex == left) {
            // 更新最大值的位置
            maxIndex = minIndex;
        }
        swap(array, maxIndex, right);
        left++;
        right--;
    }
}
Copy after login
  • 时间复杂度分析:虽然是优化了,但去小项之后,还是 O(n^2)
  • 空间复杂度分析:O(1)
  • 稳定性:不稳定
  • 实际开发中用的不多

6、堆排序

如果你有学习过优先级队列,或者看过博主优先级队列的文章,那么这个排序对于你来说还是很轻松的,当然在堆排序的讲解中,不会过多的去介绍堆的概念,如果对这部分概念还不理解,可以移至博主的上一篇文章进行学习。

堆排序,简单来说,就是把一组数据,看成一个完全二叉树,再把这棵树,建大堆或者建小堆,接着进行排序的一种思路。至于如何建大堆或小堆,和向上调整算法以及向下调整算法,这里也不多介绍了,博主的上篇文章都详细介绍过。

这里我们来分析一下,排升序应该建什么堆?大堆!排降序建小堆!

这里我们来排升序,建大堆,因为大堆堆顶元素一定是堆中最大的,所以我们可以把堆顶元素和最后一个元素进行交换,这样我们就确认了最大值的位置,接着将交换后的堆顶元素进行向下调整,仍然使得该数组满足大堆的特性!图解如下:

如上图步骤也很简单,先是将数组建成大堆,然后利用大堆来进行堆排序,首先将堆顶元素和最后一个元素交换,由此最大的元素就有序了,接着将该堆进行向下调整,使继续满足大堆性质,依次进行下去即可。

代码实现:

public void heapSort(int[] array) {
    // 建大堆 从最后一个非叶子节点开始向下调整
    // 非叶子节点下标 = (孩子节点下标 - 1) / 2
    for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
        shiftDown(array, parent, array.length);
    }
    // 建大堆完成后, 每次堆顶元素与最后一个元素交换, 锁定最大元素的位置
    for (int len = array.length - 1; len > 0; len--) {
        swap(array, 0, len); //根节点与最后一个元素交换
        shiftDown(array, 0, len); //根节点位置向下调整
    }
}
private void shiftDown(int[] array, int parent, int len) {
    int child = parent * 2 + 1;
    while (child < len) {
        if (child + 1 < len && array[child + 1] > array[child]) {
            child++;
        }
        // 判断父节点是否大于较大的孩子节点
        if (array[parent] < array[child]) {
            swap(array, parent, child);
            // 更新下标的位置
            parent = child;
            child = parent * 2 + 1;
        } else {
            return;
        }
    }
}
Copy after login
  • 时间复杂度分析:建堆的时间复杂度优先级队列那期有说过为 O(n),排序调整堆的时候,一共要调整 n-1 次,每次向下调整的时间复杂度是 logn,所以即 logn(n - 1),即 O(n*logn),加上面建堆的时间复杂度:O(n) + O(n*logn),最终时间复杂度也就是:O(n*logn)。
  • 空间复杂度分析:O(1)
  • 稳定性:不稳定

推荐学习:《java视频教程

The above is the detailed content of Common sorting algorithms for Java data structures (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template