简体   繁体   English

这是什么排序算法?

[英]What sorting algorithm is this?

Update: OK I see it's a bubble sort, but is it less efficient because it doesn't stop when there's no swap on a particular run? 更新:好的我看到它是一个冒泡排序,但效率较低,因为当特定运行没有交换时它不会停止? It runs until first is null. 它运行直到first为null。

Hi, I have a sorting algorithm as follows. 嗨,我有一个排序算法如下。 My question is, which sorting algorithm is this? 我的问题是,哪种排序算法是这样的? I thought it was bubble sort, but it does not do multiple runs. 我认为这是冒泡排序,但它没有多次运行。 Any idea? 任何想法? Thanks! 谢谢!

//sorting in descending order
struct node
{
    int value;
    node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
{
    node* first,second,temp;
    first= Head;
    while(first!=null)
    {
        second=first->NEXT;
        while(second!=null)
        {
            if(first->value < second->value)
            {
                temp = new node();
                temp->value=first->value;
                first->value=second->value;
                second->value=temp->value;
                delete temp;
            }
            second=second->NEXT;
        }

        first=first->NEXT;
    }
}

Let's make the algorithm clearer: 让我们使算法更清晰:

Sort {
   first = head;
   while (first ≠ NULL) {
      next = first.next
      while (next ≠ NULL) {
         if (first.value < next.value)
            swap first.value and next.value
         advance next
      }
      advance first
   }
}

This is a very inefficient implementation of insertion sort. 这是一种非常低效的插入排序实现。


Example run revealing the insertion sort characteristics: 示例运行显示插入排序特征:

5 → 2 → 3 → 1 → nil
^   ^
f   n [swap]

2 → 5 → 3 → 1 → nil
^       ^
f       n

2 → 5 → 3 → 1 → nil
^           ^
f           n [swap]

1 → 5 → 3 → 2 → nil
^               ^
f               n

1 → 5 → 3 → 2 → nil   // insert the minimum value 1 to the beginning of the sublist
    ^   ^
    f   n [swap]

1 → 3 → 5 → 2 → nil
    ^       ^
    f       n [swap]

1 → 2 → 5 → 3 → nil  // insert the minimum value 2 to the beginning of the sublist
    ^           ^
    f           n

1 → 2 → 5 → 3 → nil
        ^   ^
        f   n [swap]

1 → 2 → 3 → 5 → nil  // insert the minimum value 3 to the beginning of the sublist
        ^       ^
        f       n

1 → 2 → 3 → 5 → nil  // insert the minimum value 5 to the beginning of the sublist
            ^   ^
            f   n

1 → 2 → 3 → 5 → nil
                ^
                f

This is a kind of a hybrid between a 'classic' bubble sort and a selection sort - but closer to the classic bubble sort. 这是一种“经典”冒泡排序选择排序之间的混合 - 但更接近于经典的冒泡排序。

In the classic bubble sort, the inner loop swaps adjacent pairs as it walks the list/array. 在经典的冒泡排序中,内部循环在列表/数组行走时交换相邻的对。

In the classic selection sort, the inner loop keeps track of the largest value it finds in the remaining portion of the list, and swaps it with the first value in the portion of the list that the inner loop is currently considering. 在经典选择排序中,内循环跟踪它在列表的剩余部分中找到的最大值,并将其与内循环当前正在考虑的列表部分中的第一个值交换。

The sort as posted in the question is like the Selection sort in that the swap is always performed with the first value in the sub-list that the inner loop is considering. 问题中发布的排序类似于Selection排序,因为始终使用内循环正在考虑的子列表中的第一个值执行交换。 It's different from the Selection sort (and is similar to the classic Bubble sort) in that it performs a swap whenever it finds a value larger than the current first member of the inner loop's sub-list. 它与Selection排序(与经典冒泡排序类似)的不同之处在于,只要找到大于内循环子列表的当前第一个成员的值,它就会执行交换。

However, it's different than the classic Bubble sort in that it's not swapping adjacent pairs. 然而,它与经典的Bubble排序不同之处在于它不会交换相邻的对。 In the classic Bubble sort, when the inner loop has finished working a round, the largest element of the list has filtered to the bottom of the list, but in this variation, the smallest element has filtered to the top of the inner-loop's sub-list. 在经典冒泡排序中,当内循环完成圆形工作时,列表中最大的元素已过滤到列表的底部,但在此变体中,最小元素已过滤到内循环的子顶部-list。

I'd label this more a variation of the classic Bubble sort rather than the Selection sort because the performance characteristics of the sort in the question are the same as the classic Bubble sort ( O(n^2) comparisons and O(n^2) swaps), while a Selection sort has O(n) swaps. 我将此标记为经典冒泡排序的变体而不是选择排序,因为问题中排序的性能特征与经典冒泡排序( O(n^2)比较和O(n^2)交换),而选择排序有O(n)交换。

But, one other difference between the classic Bubble sort and this one is that a classic Bubble sort is stable , while the sort in the question isn't. 但是,经典冒泡排序与此之间的另一个区别是经典的冒泡排序是稳定的 ,而问题中的排序则不是。 Consider the following list of items when run through the sort. 通过排序运行时,请考虑以下项目列表。 Only the numbers are used in the comparison - the letters are used just to distinguish between the elements that have the same rank. 在比较中仅使用数字 - 字母仅用于区分具有相同等级的元素。 The diagrams show the swap operations performed (in the interest of brevity, the comparisons aren't shown): 图表显示了执行的交换操作(为简洁起见,未显示比较):

3.a  3.b   3.c   2.a   2.b   1.a
 ^                ^
 +----------------+


2.a  3.b   3.c   3.a   2.b   1.a
 ^                            ^
 +----------------------------+


1.a  3.b   3.c   3.a   2.b   2.a
      ^                 ^
      +-----------------+


1.a  2.b   3.c   3.a   3.b   2.a
            ^                 ^
            +-----------------+


1.a  2.b   2.a   3.a   3.b   3.c

Note that at the end of the sort, the relative position of items 2.a and 2.b have changed, indicating a non-stable sort. 请注意,在排序结束时,项目2.a和2.b的相对位置已更改,表示不稳定的排序。

This is pretty much bubble sort. 这几乎是冒泡的。 Bubble sort performed on a linked list where the values are swapped. 在交换值的链表上执行冒泡排序。 The checks node!=null is to confirm whether the end is reached or not. 检查node!=null是确认是否到达结束。

Insertion sort 插入排序

It's very similar to a bubble sort, except that instead of wapping adjacent pairs of items, you move the smallest item to the head of the list, and then the next smallest item into the second position, and so on. 它与冒泡排序非常相似,不同之处在于,不是将相邻的项目对移动,而是将最小的项目移动到列表的头部,然后将下一个最小的项目移动到第二个位置,依此类推。

Its similar to selection sort . 它类似于选择排序 In selection sort we find the min value in the list and swap with the first element and repeat the same for other elements in the list. 在选择排序中,我们在列表中找到min值并与第一个元素交换,并对列表中的其他元素重复相同的操作。 But there we are not swapping after finding the min element, instead everytime we find an element smaller than the first element ( in the first pass) we swap it with the 1st element. 但是在找到min元素之后我们没有交换,而是每当我们找到一个小于第一个元素的元素时(在第一个传递中),我们将它与第一个元素交换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM