I use the following algorithm for insertion sort:
def insertionSort(A):
indices = [z for z in xrange(len(A))]
for j in range(1, len(A)):
key = A[j]
i = j-1
while (i>=0) and (A[i]<key):
A[i+1] = A[i]
indices[j-i-1] = i+1
i = i-1
A[i+1] = key
However, I need to maintain a list to map the indices of the original values of A to the sorted values of A, which means if I have a list of [1,3,4,2] after sorting the list = [4,3,2,1] i will have a indices list of [3,1,0,2].
Any pointers? I’m kinda stuck.
EDITED: apologies, sorting in descending order..
Why are you writing a sort? Use Python’s builtin sorting.