def indirectInsertion(F, ordering, x):
    """
    Inserts x in F array ordered indirectly by ordering.
    """
    L = len(ordering)
    
    #Perform check,
    if L != len(F):
        raise ValueError
    k = L-1
    save = ordering[k] # index to space for x
    
    while k >= 0 and x < F[ordering[k]]:
        ordering[k] = ordering[k-1]
        k -= 1
    k = min(L-1, k+1)
    # print "position to insert x", k
    ordering[k] = save
    F[save] = x
    ordering[k] = save

    Inserts x in F array ordered indirectly by ordering.
    
recipes/Python/552742_Indirect_insertivalue/recipe-552742.py