Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Insertion sort

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:
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)
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:
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]
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.
 
Solution
Hello!
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)
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:
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]
I've tried OpenAI and...
Hello!
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)
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:
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]
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.
To achieve the output format you desire, where it prints the number of swaps at each step, you can simply move the print statement inside the loop where you count the swaps. Here's your modified code:

Python:
number_list = [5, 3, 1, 2, 4, 7, 6]
bytter = 0

for i in range(1, len(number_list)):
j = i
swaps_this_iteration = 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_iteration += 1
j -= 1
bytter += swaps_this_iteration
print('Antall bytter:', swaps_this_iteration)

print('Sortert:', number_list)

Now, within the while loop, it prints the number of swaps that occurred in that specific iteration. This should produce output similar to what you've specified in the code section.
 
Solution
To achieve the output format you desire, where it prints the number of swaps at each step, you can simply move the print statement inside the loop where you count the swaps. Here's your modified code:

Python:
number_list = [5, 3, 1, 2, 4, 7, 6]
bytter = 0

for i in range(1, len(number_list)):
j = i
swaps_this_iteration = 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_iteration += 1
j -= 1
bytter += swaps_this_iteration
print('Antall bytter:', swaps_this_iteration)

print('Sortert:', number_list)

Now, within the while loop, it prints the number of swaps that occurred in that specific iteration. This should produce output similar to what you've specified in the code section.
I swear I tried to insert the print statements at every step and at some point had it where you said it should be. Anyway I copied your code and did tweak the indentation as it seems like it got erased on here, but I got the wanted output now! Thank you so much! Did not expect to get the answer the very same day! Thank you!

Here's the code for future reference and potential help for others. Notice: the variables are both in Norwegian and English.
Python:
number_list = [5, 3, 1, 2, 4, 7, 6]
bytter = 0

for i in range(1, len(number_list)):
    j = i
    swaps_this_iteration = 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_iteration += 1
        j -= 1
        bytter += swaps_this_iteration
    print('Antall bytter:', swaps_this_iteration)

print('Sortert:', number_list)
 
I swear I tried to insert the print statements at every step and at some point had it where you said it should be. Anyway I copied your code and did tweak the indentation as it seems like it got erased on here, but I got the wanted output now! Thank you so much! Did not expect to get the answer the very same day! Thank you!

Here's the code for future reference and potential help for others. Notice: the variables are both in Norwegian and English.
Python:
number_list = [5, 3, 1, 2, 4, 7, 6]
bytter = 0

for i in range(1, len(number_list)):
    j = i
    swaps_this_iteration = 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_iteration += 1
        j -= 1
        bytter += swaps_this_iteration
    print('Antall bytter:', swaps_this_iteration)

print('Sortert:', number_list)
You're very welcome! I'm glad to hear that the solution worked for you, and thank you for sharing the modified code.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom