HTML & JS
def DNF(input, length, Swaps=0):
        high = length - 1
        p = 0
        i = 0
        while i <= high:
                if input[i] == 1:
                        if input[i]==input[p]:
                            p+=1
                            i+=1
                        else:
                            input[i],input[p]=input[p],input[i]
                            p = p+1
                            i = i+1
                            Swaps+=1
                elif input[i] > 2:
                        if input[i]==input[high]:
                            high-=1
                        else:
                            input[i],input[high]=input[high],input[i]
                            high = high-1
                            Swaps+=1
                else:
                        i = i+1
        print "Swaps",Swaps

input = [1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4]
DNF(input,len(input))
print input

Generated URL:

Output:

Swaps 6
[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4]
Report Bug