📜  稳定选择排序

📅  最后修改于: 2021-05-04 18:08:32             🧑  作者: Mango

如果具有相同或相同键的两个对象在排序输出中以相同顺序出现在它们要出现在要排序的输入数组中的情况下,则认为排序算法是稳定的。

可以通过更改键比较操作将本质上不稳定的任何基于比较的排序算法修改为稳定的,以便两个键的比较将位置视为具有相同键的对象的因素,或者通过对其进行调整的方式对其进行调整含义没有改变,它也变得稳定。

例子 :

Note: Subscripts are only used for understanding the concept.

Input : 4A 5 3 2 4B 1
Output : 1 2 3 4B 4A 5

Stable Selection Sort would have produced
Output : 1 2 3 4A 4B 5

选择排序的工作原理是找到最小元素,然后通过与位于该最小元素位置的元素交换,将其插入正确的位置。这就是使其不稳定的原因。

交换可能会影响将键(例如A)推到大于等于键(例如B)的位置。这使它们失去了预期的顺序。
在上面的示例中,将4 A推到4 B之后,并且在完成排序之后,将这4 A保留在这4 B之后。因此导致不稳定。

如果将最小元素放置在其位置而不进行交换,即通过将每个元素向前推一个数字将其放置在其位置,则可以使选择排序稳定。
简单来说,使用插入排序之类的技术,这意味着将元素插入正确的位置。

举例说明:

Example: 4A 5 3 2 4B 1
         First minimum element is 1, now instead
         of swapping. Insert 1 in its correct place 
         and pushing every element one step forward
         i.e forward pushing.
         1 4A 5 3 2 4B
         Next minimum is 2 :
         1 2 4A 5 3 4B
         Next minimum is 3 :
         1 2 3 4A 5 4B
         Repeat the steps until array is sorted.
         1 2 3 4A 4B 5
C++
// C++ program for modifying Selection Sort
// so that it becomes stable.
#include 
using namespace std;
  
void stableSelectionSort(int a[], int n)
{
    // Iterate through array elements
    for (int i = 0; i < n - 1; i++) 
    {
  
        // Loop invariant : Elements till a[i - 1]
        // are already sorted.
  
        // Find minimum element from 
        // arr[i] to arr[n - 1].
        int min = i;
        for (int j = i + 1; j < n; j++)
            if (a[min] > a[j])
                min = j;
  
        // Move minimum element at current i.
        int key = a[min];
        while (min > i) 
        {
            a[min] = a[min - 1];
            min--;
        }
        a[i] = key;
    }
}
  
void printArray(int a[], int n)
{
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
}
  
// Driver code
int main()
{
    int a[] = { 4, 5, 3, 2, 4, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    stableSelectionSort(a, n);
    printArray(a, n);
    return 0;
}


Java
// Java program for modifying Selection Sort
// so that it becomes stable.
class GFG
{
    static void stableSelectionSort(int[] a, int n)
    {
        // Iterate through array elements
        for (int i = 0; i < n - 1; i++) 
        {
  
            // Loop invariant : Elements till 
            // a[i - 1] are already sorted.
  
            // Find minimum element from 
            // arr[i] to arr[n - 1].
            int min = i;
            for (int j = i + 1; j < n; j++)
                if (a[min] > a[j])
                    min = j;
  
            // Move minimum element at current i.
            int key = a[min];
            while (min > i) 
            {
                a[min] = a[min - 1];
                min--;
            }
              
            a[i] = key;
        }
    }
  
    static void printArray(int[] a, int n)
    {
        for (int i = 0; i < n; i++)
        System.out.print(a[i]+ " ");
          
        System.out.println();
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        int[] a = { 4, 5, 3, 2, 4, 1 };
        int n = a.length;
        stableSelectionSort(a, n);
        printArray(a, n);
    }
}
  
// This code is contributed by Mr. Somesh Awasthi


Python3
# Python3 program for modifying Selection Sort
# so that it becomes stable.
def stableSelectionSort(a, n):
      
    # Traverse through all array elements
    for i in range(n):
  
        # Find the minimum element in remaining
        # unsorted array
        min_idx = i
        for j in range(i + 1, n):
            if a[min_idx] > a[j]:
                min_idx = j
  
        # Move minimum element at current i
        key = a[min_idx]
        while min_idx > i:
            a[min_idx] = a[min_idx - 1]
            min_idx -= 1
        a[i] = key
  
def printArray(a, n):
    for i in range(n):
        print("%d" %a[i], end = " ")
      
# Driver Code
a = [4, 5, 3, 2, 4, 1]
n = len(a)
stableSelectionSort(a, n)
printArray(a, n)
  
# This code is contributed 
# by Mr. Raju Pitta


C#
// C# program for modifying Selection Sort
// so that it becomes stable.
using System;
  
class GFG
{
    static void stableSelectionSort(int[] a, int n)
    {
        // Iterate through array elements
        for (int i = 0; i < n - 1; i++) 
        {
  
            // Loop invariant : Elements till 
            // a[i - 1] are already sorted.
  
            // Find minimum element from 
            // arr[i] to arr[n - 1].
            int min = i;
            for (int j = i + 1; j < n; j++)
                if (a[min] > a[j])
                    min = j;
  
            // Move minimum element at current i.
            int key = a[min];
            while (min > i) 
            {
                a[min] = a[min - 1];
                min--;
            }
              
            a[i] = key;
        }
    }
  
    static void printArray(int[] a, int n)
    {
        for (int i = 0; i < n; i++)
        Console.Write(a[i] + " ");
          
        Console.WriteLine();
    }
  
    // Driver code
    public static void Main () 
    {
        int[] a = { 4, 5, 3, 2, 4, 1 };
        int n = a.Length;
        stableSelectionSort(a, n);
        printArray(a, n);
    }
}
  
// This code is contributed by vt_m.


输出:

1 2 3 4 4 5