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

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, 92, 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:

## Python

`# Python 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)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Worst Time Complexity:** O(N^{2})

**Best Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Insertion sort to sort even and odd positioned elements in different orders
- Program to sort an array of strings using Selection Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort
- Sort an array containing two types of elements
- Sort array after converting elements to their squares
- Sort elements of array whose modulo with K yields P
- Sort elements of the array that occurs in between multiples of K
- Sort an almost sorted array where only two elements are swapped
- Sort an array by swapping elements of different type specified by another array
- Check if it is possible to sort an array with conditional swapping of elements at distance K
- Sort an Array based on the absolute difference of adjacent elements
- Sort the linked list in the order of elements appearing in the array
- Sort array such that absolute difference of adjacent elements is in increasing order
- Count of smaller elements on right side of each element in an Array using Merge sort
- Number of steps to sort the array by changing order of three elements in each step
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Serial Sort v/s Parallel Sort in Java

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.