📌  相关文章
📜  在开始添加元素以对数组进行排序|斯大林分类的变化

📅  最后修改于: 2021-04-29 01:51:09             🧑  作者: Mango

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

例子:

方法:想法是,只要元素小于前一个元素,就将其推入数组的开头。重复此步骤,直到没有不正确顺序的元素。

下面是上述方法的实现:

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.


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

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