Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To explain why your script isn't working right now, I'll rename the variable <code>unsorted</code> to <code>sorted</code>.</p> <p>At first, your list isn't yet sorted. Of course, we set <code>sorted</code> to <code>False</code>.</p> <p>As soon as we start the <code>while</code> 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 <code>sorted</code> back to <code>False</code>. <code>sorted</code> will remain <code>True</code> <em>only if there were no elements in the wrong order</em>.</p> <pre><code>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] &gt; 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. </code></pre> <p>There are also minor little issues that would help the code be more efficient or readable.</p> <ul> <li><p>In the <code>for</code> loop, you use the variable <code>element</code>. Technically, <code>element</code> 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 <code>i</code> for "index".</p> <pre><code>for i in range(0, length): </code></pre></li> <li><p>The <code>range</code> command can also take just one argument (named <code>stop</code>). In that case, you get a list of all the integers from 0 to that argument.</p> <pre><code>for i in range(length): </code></pre></li> <li><p>The <a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer" title="Style Guide for Python Code">Python Style Guide</a> 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.</p> <pre><code>def bubble(bad_list): </code></pre></li> <li><p>To swap the values of two variables, write them as a tuple assignment. The right hand side gets evaluated as a tuple (say, <code>(badList[i+1], badList[i])</code> is <code>(3, 5)</code>) and then gets assigned to the two variables on the left hand side (<code>(badList[i], badList[i+1])</code>).</p> <pre><code>bad_list[i], bad_list[i+1] = bad_list[i+1], bad_list[i] </code></pre></li> </ul> <p>Put it all together, and you get this:</p> <pre><code>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] &gt; 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 </code></pre> <p>(I removed your print statement too, by the way.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload