簡體   English   中英

這是什么排序算法?

[英]What sorting algorithm is this?

更新:好的我看到它是一個冒泡排序,但效率較低,因為當特定運行沒有交換時它不會停止? 它運行直到first為null。

嗨,我有一個排序算法如下。 我的問題是,哪種排序算法是這樣的? 我認為這是冒泡排序,但它沒有多次運行。 任何想法? 謝謝!

//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;
    }
}

讓我們使算法更清晰:

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
   }
}

這是一種非常低效的插入排序實現。


示例運行顯示插入排序特征:

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

這是一種“經典”冒泡排序選擇排序之間的混合 - 但更接近於經典的冒泡排序。

在經典的冒泡排序中,內部循環在列表/數組行走時交換相鄰的對。

在經典選擇排序中,內循環跟蹤它在列表的剩余部分中找到的最大值,並將其與內循環當前正在考慮的列表部分中的第一個值交換。

問題中發布的排序類似於Selection排序,因為始終使用內循環正在考慮的子列表中的第一個值執行交換。 它與Selection排序(與經典冒泡排序類似)的不同之處在於,只要找到大於內循環子列表的當前第一個成員的值,它就會執行交換。

然而,它與經典的Bubble排序不同之處在於它不會交換相鄰的對。 在經典冒泡排序中,當內循環完成圓形工作時,列表中最大的元素已過濾到列表的底部,但在此變體中,最小元素已過濾到內循環的子頂部-list。

我將此標記為經典冒泡排序的變體而不是選擇排序,因為問題中排序的性能特征與經典冒泡排序( O(n^2)比較和O(n^2)交換),而選擇排序有O(n)交換。

但是,經典冒泡排序與此之間的另一個區別是經典的冒泡排序是穩定的 ,而問題中的排序則不是。 通過排序運行時,請考慮以下項目列表。 在比較中僅使用數字 - 字母僅用於區分具有相同等級的元素。 圖表顯示了執行的交換操作(為簡潔起見,未顯示比較):

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

請注意,在排序結束時,項目2.a和2.b的相對位置已更改,表示不穩定的排序。

這幾乎是冒泡的。 在交換值的鏈表上執行冒泡排序。 檢查node!=null是確認是否到達結束。

插入排序

它與冒泡排序非常相似,不同之處在於,不是將相鄰的項目對移動,而是將最小的項目移動到列表的頭部,然后將下一個最小的項目移動到第二個位置,依此類推。

它類似於選擇排序 在選擇排序中,我們在列表中找到min值並與第一個元素交換,並對列表中的其他元素重復相同的操作。 但是在找到min元素之后我們沒有交換,而是每當我們找到一個小於第一個元素的元素時(在第一個傳遞中),我們將它與第一個元素交換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM