Minimize the Permutation (Codeforces 1256B)
Tags: Greedy, greedy

You are given a permutation of length $$$n$$$. Recall that the permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).You can perform at most $$$n-1$$$ operations with the given permutation (it is possible that you don't perform any operations at all). The $$$i$$$-th operation allows you to swap elements of the given permutation on positions $$$i$$$ and $$$i+1$$$. Each operation can be performed at most once. The operations can be performed in arbitrary order.Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.You can see the definition of the lexicographical order in the notes section.You have to answer $$$q$$$ independent test cases.For example, let's consider the permutation $$$[5, 4, 1, 3, 2]$$$. The minimum possible permutation we can obtain is $$$[1, 5, 2, 4, 3]$$$ and we can do it in the following way:  perform the second operation (swap the second and the third elements) and obtain the permutation $$$[5, 1, 4, 3, 2]$$$;  perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation $$$[5, 1, 4, 2, 3]$$$;  perform the third operation (swap the third and the fourth elements) and obtain the permutation $$$[5, 1, 2, 4, 3]$$$.  perform the first operation (swap the first and the second elements) and obtain the permutation $$$[1, 5, 2, 4, 3]$$$; Another example is $$$[1, 2, 4, 3]$$$. The minimum possible permutation we can obtain is $$$[1, 2, 3, 4]$$$ by performing the third operation (swap the third and the fourth elements).

Input: The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of test cases. Then $$$q$$$ test cases follow.The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of elements in the permutation.The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the given permutation.

Output: For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.

Note: Recall that the permutation $$$p$$$ of length $$$n$$$ is lexicographically less than the permutation $$$q$$$ of length $$$n$$$ if there is such index $$$i \le n$$$ that for all $$$j$$$ from $$$1$$$ to $$$i - 1$$$ the condition $$$p_j = q_j$$$ is satisfied, and $$$p_i < q_i$$$. For example:  $$$p = [1, 3, 5, 2, 4]$$$ is less than $$$q = [1, 3, 5, 4, 2]$$$ (such $$$i=4$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$),  $$$p = [1, 2]$$$ is less than $$$q = [2, 1]$$$ (such $$$i=1$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$). 

Sample Inputs:
4
5
5 4 1 3 2
4
1 2 4 3
1
1
4
4 3 2 1

Sample Outputs:
1 5 2 4 3 
1 2 3 4 
1 
1 4 3 2

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)

def solve(n, a):
    m = [float('inf')] * n
    m[-1] = a[-1]
    for i in range(n - 2, -1, -1):
        m[i] = min(a[i], m[i + 1])
    p = -1
    for i in range(n - 1):
        if m[i] < m[i + 1]:
            j = i
            if p < 0:
                while j > 0:
                    if a[j] > a[j - 1]:
                        break
                    (a[j - 1], a[j]) = (a[j], a[j - 1])
                    j -= 1
                p = i
            else:
                while j > p:
                    if a[j] > a[j - 1]:
                        break
                    (a[j - 1], a[j]) = (a[j], a[j - 1])
                    j -= 1
                p = i
    j = n - 1
    while j > max(p, 0):
        if a[j] > a[j - 1]:
            break
        (a[j - 1], a[j]) = (a[j], a[j - 1])
        j -= 1
    return a
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    res2 = solve(n, a)
    print(*res2)

Solution (Python):
T = int(input())
for _ in range(T):
    n = int(input())
    arr = list(map(int, input().split()))
    count = 0
    while count < n:
        index = count
        for i in range(count, n):
            if arr[i] < arr[index]:
                index = i
        for i in range(index - 1, count - 1, -1):
            temp = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = temp
        if index == count:
            count += 1
        else:
            count = index
    print(*arr)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input().split()]
    pre = [n - 1] * len(arr)
    for i in range(n - 2, -1, -1):
        if arr[i] <= arr[pre[i + 1]]:
            pre[i] = i
        else:
            pre[i] = pre[i + 1]
    start = 0
    c = 0
    while start < n - 1:
        if pre[start] == start:
            start += 1
            continue
        j = pre[start]
        x = arr[j]
        for k in range(j, start, -1):
            arr[k] = arr[k - 1]
        arr[start] = x
        start = j
        pre[start] = start
        if start + 1 < n:
            if arr[start] > arr[pre[start + 1]]:
                pre[start] = pre[start + 1]
    print(*arr)