I have this insertion sorting algorithm that produces a j
, i
, key
, and A
. All of these I want to store in a separate list. But when I try to store A
the nested list returns this.
store_j = []
store_i = []
store_key = []
store_A = []
def insertion_sort(A):
for j in range(1, len(A)):
key = A[j]
i = j - 1
store_j.append(j)
store_i.append(i)
store_key.append(key)
store_A.append(A)
while i >= 0 and A[i] > key:
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
print(store_i)
print(store_j)
print(store_key)
print(store_A)
a = [5, 1, 3, 4, 2]
insertion_sort(a)
[0, 1, 2, 3]
[1, 2, 3, 4]
[1, 3, 4, 2]
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
I want the last store_A to output like this
[[5, 1, 3, 4, 2][1, 5, 3, 4, 2][1, 3, 5, 4, 2][1, 3, 4, 5, 2][1, 2, 3, 4, 5]]
So I can eventually make a table with the stored values
Something like this
j | i | key| A |
1 | 0 | 1 |[5, 1, 3, 4, 2]|
2 | 1 | 3 |[1, 5, 3, 4, 2]|
3 | 2 | 4 |[1, 3, 5, 4, 2]|
4 | 3 | 2 |[1, 3, 4, 5, 2]|
4 | 0 | 2 |[1, 2, 3, 4, 5]|