📜  稳定的降序排序

📅  最后修改于: 2021-05-04 19:14:07             🧑  作者: Mango

给定一个由n个整数组成的数组,我们必须对数组元素进行反向排序,以便等键在排序后保持稳定。
例子:

Input : arr[] = {4, 2, 3, 2, 4}
Output : 4, 4, 3, 2, 2

先决条件:排序算法的稳定性

方法1(编写我们自己的排序函数:冒泡排序)

我们知道,像冒泡排序,插入排序,合并排序,计数排序这样的排序算法是稳定的。我们在这里实现冒泡排序。

解释
首过
(4′,2′,3,2″,4″)->(2′,4′,3,4″,2″)这里的算法比较最后两个元素和
从2“ <4”开始交换。
(2’,4’,3,4”,2”)->(2’,4’,4”,3,2”)交换,因为3 <4“
(2′,4′,4″,3、2″)->(2′,4′,4″,3、2″)
(2’,4’,4”,3,2”)->(4’,2’,4”,3,2”)交换,因为2′<4’。

第二遍:
(4′,2′,4″,3、2″)->(4′,2′,4″,3、2″)
(4′,2′,4″,3、2″)->(4′,2′,4″,3、2″)
(4’,2’,4”,3,2”)->(4’,4”,2’,3,2”)交换自2’(4’,4”,2’,3,2” )

第三关:
(4′,4″,2′,3,2″)->(4′,4″,2′,3,2″)
(4’,4”,2’,3,2”)->(4’,4”,3,2’,2”)交换,因为2′<3
现在,该数组已排序,并且相同元素的顺序与原始数组中的顺序相同。

C++
// Bubble sort implementation to sort
// elements in descending order.
#include 
#include 
using namespace std;
  
void print(vector a, int n)
{
    for (int i = 0; i <= n; i++) 
        cout << a[i] << " ";    
    cout << endl;
}
  
// Sorts a[] in descending order using
// bubble sort.
void sort(vector a, int n)
{
    for (int i = n; i >= 0; i--) 
        for (int j = n; j > n - i; j--) 
            if (a[j] > a[j - 1]) 
                swap(a[j], a[j-1]);
    print(a, n);
}
  
// Driver code
int main()
{
    int n = 7;
    vector a;
    a.push_back(2);
    a.push_back(4);
    a.push_back(3);
    a.push_back(2);
    a.push_back(4);
    a.push_back(5);
    a.push_back(3);
    sort(a, n - 1);
    return 0;
}


Java
// Bubble sort implementation 
// to sort elements in 
// descending order.
import java.io.*;
import java.util.*;
  
class GFG
{ 
static void print(ArrayList a, 
                                 int n)
{
    for (int i = 0; i <= n; i++) 
        System.out.print(a.get(i) + " "); 
    System.out.println();
}
  
// Sorts a[] in descending 
// order using bubble sort.
static void sort(ArrayList a,
                                  int n)
{
    for (int i = n; 
            i >= 0; i--) 
        for (int j = n; 
                j > n - i; j--) 
            if (a.get(j) > a.get(j - 1)) 
            {
                int tempswap = a.get(j);
                a.remove(j);
                a.add(j, a.get(j - 1)); 
                a.remove(j - 1);
                a.add(j - 1, tempswap); 
            }
    print(a, n);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 6;
    ArrayList a = new ArrayList();
    a.add(2);
    a.add(4);
    a.add(3);
    a.add(2);
    a.add(4);
    a.add(5);
    a.add(3);
    sort(a, n);
}
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


Python3
# Bubble sort implementation to sort
# elements in descending order.
  
def print1(a, n):
  
    for i in range(0,n+1):
        print(a[i],end=" ") 
    print("")
  
  
# Sorts a[] in descending order using
# bubble sort.
def sort(a, n):
  
    for i in range(n,0,-1): 
        for j in range(n, n - i,-1): 
            if (a[j] > a[j - 1]): 
                a[j], a[j-1]=a[j-1], a[j]
    print1(a,n)
  
  
# Driver code
n = 7
a = [2,4,3,2,4,5,3]
  
sort(a, n-1)
  
# This code is contributed
# by Smitha Dinesh Semwal


C#
// Bubble sort implementation 
// to sort elements in 
// descending order.
using System;
using System.Collections.Generic;
  
class GFG
{ 
static void print(List a, 
                       int n)
{
    for (int i = 0; i <= n; i++) 
        Console.Write(a[i] + " "); 
    Console.WriteLine();
}
  
// Sorts a[] in descending 
// order using bubble sort.
static void sort(List a,
                      int n)
{
    for (int i = n; 
             i >= 0; i--) 
        for (int j = n; 
                 j > n - i; j--) 
            if (a[j] > a[j - 1]) 
            {
                int tempswap = a[j];
                a[j] = a[j - 1]; 
                a[j - 1] = tempswap; 
            }
    print(a, n);
}
  
// Driver code
static void Main()
{
    int n = 6;
    List a = new List();
    a.Add(2);
    a.Add(4);
    a.Add(3);
    a.Add(2);
    a.Add(4);
    a.Add(5);
    a.Add(3);
    sort(a, n);
}
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


PHP
= 0; $i--)
    {
        for ($j = $n; 
             $j > $n - $i; $j--) 
        {
            if ($a[$j] > $a[$j - 1]) 
                swap($a[$j], 
                     $a[$j - 1]);
        }
    }
    print1($a, $n);
}
  
// Driver code
$n = 6;
$a = array();
array_push($a, 2);
array_push($a, 4);
array_push($a, 3);
array_push($a, 2);
array_push($a, 4);
array_push($a, 5);
array_push($a, 3);
sort1($a, $n);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>


C++
// C++ program to demonstrate descending order
// stable sort using greater<>().
#include 
using namespace std;
  
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    stable_sort(arr, arr + n, greater());
  
    cout << "Array after sorting : \n";
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
  
    return 0;
}


Java
// Java program to demonstrate descending order 
// stable sort using greater<>().
import java.util.*;
  
class GFG 
{
    static void reverse(int a[]) 
    { 
        int i, k, n = a.length; 
        int t;
        for (i = 0; i < n / 2; i++)
        { 
            t = a[i]; 
            a[i] = a[n - i - 1]; 
            a[n - i - 1] = t; 
        } 
    }
      
    // Driver code 
    public static void main(String[] args)
    {
        int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
        int n = arr.length;
  
        Arrays.sort(arr);
        reverse(arr);
  
        System.out.println("Array after sorting : \n");
        for (int i = 0; i < n; ++i)
        {
            System.out.print(arr[i] + " ");
        }
    }
} 
  
// This code has been contributed by 29AjayKumar


Python 3
# Python 3 program to demonstrate 
# descending order
  
if __name__ == "__main__":
      
    arr = [ 1, 5, 8, 9, 6,
            7, 3, 4, 2, 0 ]
    n = len(arr)
  
    arr.sort(reverse = True)
  
    print("Array after sorting : ")
    for i in range(n):
        print(arr[i], end = " ")
  
# This code is contributed by ita_c


C#
// C# program to demonstrate descending order 
using System;
      
class GFG 
{
    static void reverse(int []a) 
    { 
        int i, k, n = a.Length; 
        int t;
        for (i = 0; i < n / 2; i++)
        { 
            t = a[i]; 
            a[i] = a[n - i - 1]; 
            a[n - i - 1] = t; 
        } 
    }
      
    // Driver code 
    public static void Main(String[] args)
    {
        int []arr = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
        int n = arr.Length;
  
        Array.Sort(arr);
        reverse(arr);
  
        Console.WriteLine("Array after sorting : \n");
        for (int i = 0; i < n; ++i)
        {
            Console.Write(arr[i] + " ");
        }
    }
} 
  
// This code is contributed by 29AjayKumar


输出:

5 4 4 3 3 2 2

方法2(使用库函数)
我们可以使用stable_sort对元素进行稳定排序。

C++

// C++ program to demonstrate descending order
// stable sort using greater<>().
#include 
using namespace std;
  
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    stable_sort(arr, arr + n, greater());
  
    cout << "Array after sorting : \n";
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
  
    return 0;
}

Java

// Java program to demonstrate descending order 
// stable sort using greater<>().
import java.util.*;
  
class GFG 
{
    static void reverse(int a[]) 
    { 
        int i, k, n = a.length; 
        int t;
        for (i = 0; i < n / 2; i++)
        { 
            t = a[i]; 
            a[i] = a[n - i - 1]; 
            a[n - i - 1] = t; 
        } 
    }
      
    // Driver code 
    public static void main(String[] args)
    {
        int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
        int n = arr.length;
  
        Arrays.sort(arr);
        reverse(arr);
  
        System.out.println("Array after sorting : \n");
        for (int i = 0; i < n; ++i)
        {
            System.out.print(arr[i] + " ");
        }
    }
} 
  
// This code has been contributed by 29AjayKumar

的Python 3

# Python 3 program to demonstrate 
# descending order
  
if __name__ == "__main__":
      
    arr = [ 1, 5, 8, 9, 6,
            7, 3, 4, 2, 0 ]
    n = len(arr)
  
    arr.sort(reverse = True)
  
    print("Array after sorting : ")
    for i in range(n):
        print(arr[i], end = " ")
  
# This code is contributed by ita_c

C#

// C# program to demonstrate descending order 
using System;
      
class GFG 
{
    static void reverse(int []a) 
    { 
        int i, k, n = a.Length; 
        int t;
        for (i = 0; i < n / 2; i++)
        { 
            t = a[i]; 
            a[i] = a[n - i - 1]; 
            a[n - i - 1] = t; 
        } 
    }
      
    // Driver code 
    public static void Main(String[] args)
    {
        int []arr = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
        int n = arr.Length;
  
        Array.Sort(arr);
        reverse(arr);
  
        Console.WriteLine("Array after sorting : \n");
        for (int i = 0; i < n; ++i)
        {
            Console.Write(arr[i] + " ");
        }
    }
} 
  
// This code is contributed by 29AjayKumar

输出:

Array after sorting : 
9 8 7 6 5 4 3 2 1 0