martian94
Coder
Hello!
This time the class is tasked with insertion sorting and we've been provided with pseudocode. This far my code is this:
But where it says "9" after Antall bytter I need it to be like this when the number list is 5, 3, 1, 2, 4, 7, 6:
I've tried OpenAI and Bard, but they seem unable to understand what I mean. What can I do/add to my code so it puts out the numbers in the above "code" section? As for now I just get "Antall bytter: 9". It should put out what I've written in the code section.
This time the class is tasked with insertion sorting and we've been provided with pseudocode. This far my code is this:
Python:
number_list = [5,3,1,2,4,7,6]
bytter = 0
for i in range(1,len(number_list)):
j = i
swaps_this_far = 0
while j > 0 and number_list[j-1] > number_list[j]:
number_list[j], number_list[j-1] = number_list[j-1],number_list[j]
swaps_this_far += 1
j -= 1
bytter += swaps_this_far
print('Number of swaps:',bytter)
print('Sorted: ',number_list)
Code:
Antall bytter: 1
Antall bytter: 2
Antall bytter: 2
Antall bytter: 1
Antall bytter: 0
Antall bytter: 1
Sortert: [1, 2, 3, 4, 5, 6, 7]