📌  相关文章
📜  在 start 中添加元素对数组进行排序 |斯大林排序的变体

📅  最后修改于: 2021-09-04 13:07:32             🧑  作者: Mango

斯大林排序(也称为“独裁者排序”和“王牌排序”)是一种荒谬的“排序”算法,其中每个不正确顺序的元素都被简单地从列表中删除。
这种排序算法是斯大林排序的一种破坏性较小的变体,它实际上会对列表进行排序:在这种情况下,不按顺序排列的元素将按照它们在原始列表中出现的顺序移动到列表的开头。然后重复该过程,直到对列表进行排序。

例子:

方法:这个想法是在数组开头的元素小于前一个元素时推送元素。重复此步骤,直到没有这样的元素顺序不正确。

下面是上述方法的实现:

C++
// C++ implementation to sort the
// array by using the variation
// of the Stalin sort
#include
using namespace std;
 
// Function to sort the array
void variationStalinsort(vector arr)
{
    int j = 0;
     
    while(true)
    {
        int moved = 0;
         
        for(int i = 0;
                i < (arr.size() - 1 - j); i++)
        {
            if (arr[i] > arr[i + 1])
            {
                vector::iterator index;
                int temp;
                index = arr.begin() + i + 1;
                temp = arr[i + 1];
                arr.erase(index);
                arr.insert(arr.begin() + moved, temp);
                moved++;
            }
        }
         
        j++;
         
        if (moved == 0)
        {
            break;
        }
    }
    for(int i = 0; i < arr.size(); i++)
    {
        cout << arr[i] << ", ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 2, 1, 4, 3, 6,
                        5, 8, 7, 10, 9 };
     
    // Function call
    variationStalinsort(arr);
}
 
// This code is contributed by avanitrachhadiya2155


Java
// Java implementation to sort the
// array by using the variation
// of the Stalin sort
import java.util.*;
class GFG
{
 
  // Function to sort the array
  static void variationStalinsort(Vector arr)
  {
    int j = 0;
    while(true)
    {
      int moved = 0; 
      for(int i = 0;
          i < (arr.size() - 1 - j); i++)
      {
        if (arr.get(i) > arr.get(i+1))
        {
          //Iterator index = arr.iterator();
          int index;
          int temp;
          index = arr.get(i);
          temp = arr.get(i + 1);
          arr.removeElement(index);
          arr.add( i, temp);
          arr.removeElement(temp);
          arr.add(i+1, index);
          moved++;
        }
      }  
      j++;    
      if (moved == 0)
      {
        break;
      }
    }
    System.out.print(arr);
 
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 2, 1, 4, 3, 6,
                 5, 8, 7, 10, 9 };
    Vector arr1 = new Vector<>();
    for(int i = 0; i < arr.length; i++)
      arr1.add(arr[i]);
 
    // Function call
    variationStalinsort(arr1);
  }
}
 
// This code is contributed by aashish1995


Python3
# Python3 implementation to sort
# the array by using the variation
# of the Stalin sort
 
# Function to sort the array
def variationStalinsort(arr):
    j = 0
    while True:
        moved = 0
        for i in range(len(arr) - 1 - j):
            if arr[i] > arr[i + 1]:
                arr.insert(moved, arr.pop(i + 1))
                moved += 1
        j += 1
        if moved == 0:
            break
    return arr
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
     
    # Function Call
    print(variationStalinsort(arr))


C#
// C# implementation to sort the
// array by using the variation
// of the Stalin sort
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
 
  // Function to sort the array
  static void variationStalinsort(List arr)
  {
    int j = 0;
    while(true)
    {
      int moved = 0; 
      for(int i = 0;
          i < (arr.Count - 1 - j); i++)
      {
        if (arr[i] > arr[i+1])
        {
           
          //Iterator index = arr.iterator();
          int index;
          int temp;
          index = arr[i];
          temp = arr[i + 1];
          arr.Remove(index);
          arr.Insert(i , temp);
          arr.Remove(temp);
          arr.Insert(i + 1 ,index);
          moved++;
        }
      }  
      j++;    
      if (moved == 0)
      {
        break;
      }
    }
    foreach(int i in arr)
    Console.Write(i + " ");
 
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 2, 1, 4, 3, 6,
                 5, 8, 7, 10, 9 };
    List arr1 = new List();
    for(int i = 0; i < arr.Length; i++)
      arr1.Add(arr[i]);
 
    // Function call
    variationStalinsort(arr1);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

最差时间复杂度: O(N 2 )
最佳时间复杂度: O(N)