python - Bubble Sort Homework -


We are doing sorting algorithms in the class, and although I understand them when I'm talking about them and write cdDodo I am having problems, write the actual code for them.

This is my attempt in Python:

  mylist = [12, 5, 13, 8, 9, 65] def bubble (badList): length = lane (bad List) - 1 without systemat = floating while true: for element in range (0, length): unordered = false, if bad list [element] & gt; BadList [element + 1]: hold = badList [element + 1] badList [element + 1] = badList [element] badList [element] = hold another print badList: unclassified = true print bubble (MyList)  < / Ex> 

Now, this (as far as I can tell) is in the right way, but once it finishes, it only suffers from it indefinitely.

How can this code be fixed so that the function is properly cured and properly listed in any (appropriate) size?

I know that I really should not print in one function and I should have a return, but I have not done so because my code really does not work yet.

To find out why your script is not working right now, type the variable unsorted < / Code> to sorted .

First of all, your list has not yet been sorted. Of course, we set sorted to False .

As soon as we start the while loop, we believe that the list has already been sorted out, the idea is this: as soon as we get two elements that are in the right order No, we set the sorted back to False . Sorted Only true will remain only when there were no elements in the wrong object

  sorted = False # not sorted We have not started sorting yet: Sorted = True # Assume that the list is sorted for category (0, length) now: if badList [element] & gt; BadList [element + 1]: according to the sort = false # we found two elements in the wrong order hold = badList [element + 1] badList [element + 1] = badList [element] badList [element] found # Gone list At this point, if there were no # elements in the wrong order, sorting is still true otherwise it is wrong, and #s while the loop runs again  

there too There are minor minor problems that will help make the code more efficient or readable.

  • for loop, you use the variable element technically, element Is not an element; This is a list that represents the index. In addition, it is quite long in these situations, just use a floating variable name, such as i for "code".

      In the category (0, length):  <
     
  • class command is only one argument ( Named stop ), in that case, you get a list of all integers of 0 from that argument.

      for the range I (length) in:  
  • It recommends that the variable name in lowercase with underscore has given. This is a very small knighthood for such a small script; This is more to get accustomed to what you see like the Python code most often.

      def bubble (bad_list):.  
  • To swap, type two variables, form them as a tubal assignment. On the right is evaluated as a tubal (say, (badList [i + 1], bad list [i]) is (3, 5) ) And then it is assigned to the left side ( (badList [i], poor list [i + 1])

      bad_list [i], bad_list [i] for two variables +1] = bad_list [i + 1], bad_list [i]  

Put it together, and you get it:

< Pre => my_list = [12, 5, 13, 8, 9, 65] def bubble (bad_list): length = lane (bad_list) - 1 = Sorted by false while not sorted: Sorted Bad_list [i + 1] = bad_list [i + 1], bad_list [i] bubble (my_list): if bad_list [i] & gt; bad_list [i + 1]: sorted by false = false_list [i] Print my_list

(I removed my print statement anyway.)


Comments