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 Bubble sort with iteration print

martian94

Coder
Hello! I'm new to coding and I am in the noob phase. I am tasked with creating a bubble sort algorithm. My problem is, hopefully, quite simple to you. The iterations in the print statements seem to sort the numbers list right away, instead of printing each iteration of two numbers switching places. It should go like:

Given numbers: [5, 3, 1, 2, 4]
Iteration 1: [3, 5, 1, 2, 4] > [3, 1, 5, 2, 4] > [3, 1, 2, 5, 4] > [3, 1, 2, 4, 5]
Print 3, 1, 2, 4, 5
Iteration 2: [1, 3, 2, 4, 5] > [1, 2, 3, 4, 5] (now 3, 4 and 5 is correctly placed)
Print 1, 2, 3, 4, 5
Iteration 3: [1, 2, 3, 4, 5] (what happened here is compare 1 and 2, no need to swap)
Print 1, 2, 3, 4, 5
Final result: [1, 2, 3, 4, 5].

But it does not go this way. Instead it prints
[3, 1, 2, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Final result: [1, 2, 3, 4, 5]

[...]
Actually, no. I saw this is in fact right now. I just did the iterations on paper and I see it is right, it is just the fact that there are no switches in the last iterations which makes it look weird. It works as expected with a list [5, 4, 3, 2, 1] for example. I'll post this anyway in hopes it helps somebody else.
 

Attachments

  • Boblesorterting-iterasjoner-diagram.drawio.png
    Boblesorterting-iterasjoner-diagram.drawio.png
    195.6 KB · Views: 6
  • Boblesorterting-iterasjoner-diagram2.drawio.png
    Boblesorterting-iterasjoner-diagram2.drawio.png
    192.9 KB · Views: 6
Solution
For future reference and in order to help people, this is the code. Note that this is the least effective sorting algorithm, but it's a basic one that we are learning in a Python 101 course at university.

Python:
def boblesortering(array):
    antall_tall = len(array)

    # Print liste med tall som skal sorteres
    print('OPPGITTE TALL:',talliste)
    print('-----------------------------------------')

    #For hver iterasjon i range av talliste - 1 fordi i = n-1
    for iterasjon in range(antall_tall-1):
        for ny_indeks in range(0,antall_tall-iterasjon-1):
            # Argument for evt flytting av indeks
            if array[ny_indeks] > array[ny_indeks+1]:

                #Bytt elementer i array om ny_indeks er større ennn ny_indeks+1...
I do not have the problem anymore as I stated in the last paragraph. I did it right the first time, it just looked wrong due to it being so few index switches. Do you perhaps know how to mark this as solved? I can not find the button for that. I read somewhere here that you were supposed to mark stuff as solved.
 
I do not have the problem anymore as I stated in the last paragraph. I did it right the first time, it just looked wrong due to it being so few index switches. Do you perhaps know how to mark this as solved? I can not find the button for that. I read somewhere here that you were supposed to mark stuff as solved.
Oh right, that was not clear to me. In fact the entire post was not clear to me. But that does not matter now 😁
I seem to remember the Solved button being on the right side of the header but I could be wrong and am unable to verify this, not having an open question.
 
Oh right, that was not clear to me. In fact the entire post was not clear to me. But that does not matter now 😁
I seem to remember the Solved button being on the right side of the header but I could be wrong and am unable to verify this, not having an open question.
I see it now, it is to the right of the upvote/downvote buttons. The checkmark in a circle icon. When you hover it the alt="Mark as solution", so I see one is supposed to mark the comments as solution(s), not the thread itself.
 
For future reference and in order to help people, this is the code. Note that this is the least effective sorting algorithm, but it's a basic one that we are learning in a Python 101 course at university.

Python:
def boblesortering(array):
    antall_tall = len(array)

    # Print liste med tall som skal sorteres
    print('OPPGITTE TALL:',talliste)
    print('-----------------------------------------')

    #For hver iterasjon i range av talliste - 1 fordi i = n-1
    for iterasjon in range(antall_tall-1):
        for ny_indeks in range(0,antall_tall-iterasjon-1):
            # Argument for evt flytting av indeks
            if array[ny_indeks] > array[ny_indeks+1]:

                #Bytt elementer i array om ny_indeks er større ennn ny_indeks+1
                array[ny_indeks], array[ny_indeks+1] = array[ny_indeks+1],array[ny_indeks]

                #Print array etter hver rekkefølgeendring
                print(f'Rekkefølgeendring: ',array)
                
        # Print array etter hver iterasjon
        print(f'Gjennomgang .... {iterasjon+1}: {array}')
        

    # Print finale
    print('-----------------------------------------')
    print('Sortert ......... :',array)


talliste = [7,6,5,4,3,2,1]


#Kopier talliste til boblesortering funksjon
boblesortering(talliste.copy())
 
Solution
Back
Top Bottom