Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 214949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:25:24+00:00 2026-05-11T18:25:24+00:00

In class we are doing sorting algorithms and, although I understand them fine when

  • 0

In class we are doing sorting algorithms and, although I understand them fine when talking about them and writing pseudocode, I am having problems writing actual code for them.

This is my attempt in Python:

mylist = [12, 5, 13, 8, 9, 65]

def bubble(badList):
    length = len(badList) - 1
    unsorted = True

    while unsorted:
        for element in range(0,length):
            unsorted = False
            if badList[element] > badList[element + 1]:
                hold = badList[element + 1]
                badList[element + 1] = badList[element]
                badList[element] = hold
                print badList
            else:
                unsorted = True

print bubble(mylist)

Now, this (as far as I can tell) sorts correctly, but once it finishes it just loops indefinitely.

How can this code be fixed so the function finishes properly and correctly sorts a list of any (reasonable) size?

P.S. I know I should not really have prints in a function and I should have a return, but I just have not done that yet as my code does not really work yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:25:24+00:00Added an answer on May 11, 2026 at 6:25 pm

    To explain why your script isn’t working right now, I’ll rename the variable unsorted to sorted.

    At first, your list isn’t yet sorted. Of course, we set sorted to False.

    As soon as we start the while loop, we assume that the list is already sorted. The idea is this: as soon as we find two elements that are not in the right order, we set sorted back to False. sorted will remain True only if there were no elements in the wrong order.

    sorted = False  # We haven't started sorting yet
    
    while not sorted:
        sorted = True  # Assume the list is now sorted
        for element in range(0, length):
            if badList[element] > badList[element + 1]:
                sorted = False  # We found two elements in the wrong order
                hold = badList[element + 1]
                badList[element + 1] = badList[element]
                badList[element] = hold
        # We went through the whole list. At this point, if there were no elements
        # in the wrong order, sorted is still True. Otherwise, it's false, and the
        # while loop executes again.
    

    There are also minor little issues that would help the code be more efficient or readable.

    • In the for loop, you use the variable element. Technically, element is not an element; it’s a number representing a list index. Also, it’s quite long. In these cases, just use a temporary variable name, like i for “index”.

      for i in range(0, length):
      
    • The range command can also take just one argument (named stop). In that case, you get a list of all the integers from 0 to that argument.

      for i in range(length):
      
    • The Python Style Guide recommends that variables be named in lowercase with underscores. This is a very minor nitpick for a little script like this; it’s more to get you accustomed to what Python code most often resembles.

      def bubble(bad_list):
      
    • To swap the values of two variables, write them as a tuple assignment. The right hand side gets evaluated as a tuple (say, (badList[i+1], badList[i]) is (3, 5)) and then gets assigned to the two variables on the left hand side ((badList[i], badList[i+1])).

      bad_list[i], bad_list[i+1] = bad_list[i+1], bad_list[i]
      

    Put it all together, and you get this:

    my_list = [12, 5, 13, 8, 9, 65]
    
    def bubble(bad_list):
        length = len(bad_list) - 1
        sorted = False
    
        while not sorted:
            sorted = True
            for i in range(length):
                if bad_list[i] > bad_list[i+1]:
                    sorted = False
                    bad_list[i], bad_list[i+1] = bad_list[i+1], bad_list[i]
    
    bubble(my_list)
    print my_list
    

    (I removed your print statement too, by the way.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Most current IP stacks will not respond to a ping… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer How can use the SchemaUpdate instead of the SchemaExport, the… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer This was a whole lot easier than I first thought.… May 11, 2026 at 11:36 pm

Related Questions

I'm in a basic programming class, and everything is done in pseudo code. My
In class, we are all 'studying' databases, and everyone is using Access. Bored with
I am getting a practical issue and the issue can be dascribed as follows.
At the company I work for, I have created a Error Logging class to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.