0

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]|
martineau
  • 119,623
  • 25
  • 170
  • 301
Mandru
  • 3
  • 2
  • 1
    Add a slice to copy the list, otherwise you're just appending aliases of the same list onto the result: `store_A.append(A[:])`. – ggorlen Sep 11 '20 at 19:24
  • Does this answer your question? [How does list.append() work in Python - I'm getting an unexpected result](https://stackoverflow.com/questions/43693426/how-does-list-append-work-in-python-im-getting-an-unexpected-result) – ggorlen Sep 11 '20 at 19:27
  • You have 4 times the same object your list. To create a copy use `store_A.append(list(A))`. Also see: https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent/2612815#2612815 – Wups Sep 11 '20 at 19:30
  • Yes, now it works. Thanks for the help – Mandru Sep 11 '20 at 19:31
  • Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – Wups Sep 11 '20 at 19:32

2 Answers2

0

If I understand your question wright, you want all permutations of a list.

import numpy as np
import itertools

permutations = list(itertools.permutations([5, 2, 3, 1]))
print (permutations)    # list of tuples
perm_array = np.array(permutations)
print(perm_array)       # yields 2D array
Dharman
  • 30,962
  • 25
  • 85
  • 135
Oliver Prislan
  • 320
  • 4
  • 12
0

just make a copy of the array before you store that. otherwise you will be saving instance of the array A and it will be like storing A 5 times or len(A) times. and A will change in each iteration and while you print store_A u will get the value of A at that time. to under stand this you can add print(store_A) after store_A.append(A). this will give you

[[5, 1, 3, 4, 2]]
[[1, 5, 3, 4, 2], [1, 5, 3, 4, 2]]
[[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]
[[1, 3, 4, 5, 2], [1, 3, 4, 5, 2], [1, 3, 4, 5, 2], [1, 3, 4, 5, 2]]

Now, the fix is simple just make acope of instance a before you add to store and add a line to append(A) after the loop to append the last result.

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.copy())
        while i >= 0 and A[i] > key:
            A[i + 1] = A[i]
            i = i - 1
            A[i + 1] = key

    store_j.append(j)
    store_i.append(i)
    store_key.append(key)
    store_A.append(A.copy())
    print(store_i)
    print(store_j)
    print(store_key)
    print(store_A)

a = [5, 1, 3, 4, 2]
insertion_sort(a)

out will be like

[0, 1, 2, 3, 0]
[1, 2, 3, 4, 4]
[1, 3, 4, 2, 2]
[[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]]

and also for making table you can add the below code

template='|{0:^5}|{1:^5}|{2:^5}|{3:^18}|'
print(template.format('i','j','key','A'))
for i in range (len(store_A)):
    print(template.format(f'{store_j[i]}',f'{store_i[i]}',f'{store_key[i]}',f'{store_A[i]}'))

result will be like

|  i  |  j  | 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]  |
Adhun Thalekkara
  • 713
  • 10
  • 23