Add elements in start to sort the array | Variation of Stalin Sort - GeeksforGeeks

06 Jun, 2021
Stalin sort (also ‘dictator sort‘ and ‘trump sort‘) is a nonsensical ‘sorting’ algorithm in which each element that is not in the correct order is simply eliminated from the list. This sorting algorithm is a less destructive variation of Stalin sort, that will actually sort the list: In this case, the elements that are not in order are moved to the start of the list in the order in which they appeared in the original list. The process is then repeated until the list is sorted. 
Examples: 
Input: arr[] = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9} Output: 1 2 3 4 5 6 7 8 9 10 Explanation: Those elements which were moved to the start of the list are marked bold: [2, 1, 4, 3, 6, 5, 8, 7, 10, 9] [1, 3, 5, 7, 9 2, 4, 6, 8, 10] [2, 4, 6, 8, 1, 3, 5, 7, 9, 10] [1, 3, 5, 7, 2, 4, 6, 8, 9, 10] [2, 4, 6, 1, 3, 5, 7, 8, 9, 10] [1, 3, 5, 2, 4, 6, 7, 8, 9, 10] [2, 4, 1, 3, 5, 6, 7, 8, 9, 10] [1, 3, 2, 4, 5, 6, 7, 8, 9, 10] [2, 1, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Input: [9, 10, 7, 8, 5, 6, 3, 4, 1, 2] Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Explanation: [9, 10, 7, 8, 5, 6, 3, 4, 1, 2] [7, 8, 5, 6, 3, 4, 1, 2, 9, 10] [5, 6, 3, 4, 1, 2, 7, 8, 9, 10] [3, 4, 1, 2, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
Approach: The idea is to push the elements at the start of the array whenever it is less than the previous element. Repeat this step until there is no such element that is not in the correct order.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to sort the// array by using the variation// of the Stalin sort#include<bits/stdc++.h>using namespace std; // Function to sort the arrayvoid variationStalinsort(vector<int> 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<int>::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 Codeint main(){    vector<int> arr = { 2, 1, 4, 3, 6,                        5, 8, 7, 10, 9 };         // Function call    variationStalinsort(arr);} // This code is contributed by avanitrachhadiya2155
// Java implementation to sort the// array by using the variation// of the Stalin sortimport java.util.*;class GFG{   // Function to sort the array  static void variationStalinsort(Vector<Integer> 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<Integer> 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<Integer> 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 implementation to sort# the array by using the variation# of the Stalin sort # Function to sort the arraydef 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 Codeif __name__ == "__main__":    arr = [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]         # Function Call    print(variationStalinsort(arr))
// C# implementation to sort the// array by using the variation// of the Stalin sortusing System;using System.Collections.Generic;using System.Linq;class GFG{   // Function to sort the array  static void variationStalinsort(List<int> 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<Integer> 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<int> arr1 = new List<int>();    for(int i = 0; i < arr.Length; i++)      arr1.Add(arr[i]);     // Function call    variationStalinsort(arr1);  }} // This code is contributed by ukasp.
<script> // Javascript implementation to sort the// array by using the variation// of the Stalin sort // Function to sort the arrayfunction variationStalinsort(arr){    let j = 0;    while(true)    {        let moved = 0;        for(let i = 0;                i < (arr.length - 1 - j);                i++)        {            if (arr[i] > arr[i+1])            {                                 // Iterator<Integer> index = arr.iterator();                let index;                let temp;                index = arr[i];                temp = arr[i + 1];                arr.splice(i, 1);                arr.splice(i, 0, temp);                arr[i] = temp;                arr.splice(i + 1, 1);                arr.splice(i + 1, 0, index)                arr[i + 1] = index;                moved++;            }        }         j++;           if (moved == 0)        {            break;        }    }    document.write("[" + arr + "]");} // Driver Codelet arr = [ 2, 1, 4, 3, 6,            5, 8, 7, 10, 9 ];let arr1 = [];for(let i = 0; i < arr.length; i++)    arr1.push(arr[i]);  // Function callvariationStalinsort(arr1); // This code is contributed by rag2127 </script>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
Worst Time Complexity: O(N2) Best Time Complexity: O(N) 
avanitrachhadiya2155
aashish1995
ukasp
rag2127
Arrays
Python
School Programming
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Building Heap from Array
Program to find sum of elements in a given array
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe