What does this line mean in Sorting Algo(Bubble example)


def bubbleSort(array):
    swapped = False
    for i in range(len(array)-1,0,-1):
        for j in range(i):
            if array[j]>array[j+1]:
                array[j], array[j+1] = array[j+1], array[j]
                swapped= True
        if swapped:

bubbleSort([5, 2, 1, 3])

How should I interpret this line: for i in range(len(array)-1,0,-1)? I’m particularly confused about the need for the 0 and -1 parameters.


That line has a couple of things happening, which I will give simplified explanations of (I’m assuming this code is written in Python).

First, for i in iterable will loop through iterable, meaning the code in the for loop will repeat as many times as there are elements in iterable, which could be an array, a list, a string etc, and each time it loops, i will be the next element of iterable, starting with the first. For example, for i in [1, 2, 3] will loop 3 times; the first time, i will be equal to 1; the second, 2, etc.

Next, the range function produces an iterable that is a range of numbers, for example from 0-9. With a single argument, range will produce a range from 0 to that number, but stopping just before it, e.g. range(5) will give you [0, 1, 2, 3, 4]. Thus if you were to use for i in range(5), your code would repeat 5 times, with i incrementing from 0 to 4.

With two arguments, the range will start at the first and stop before the second, which must be greater than the first. For example, range(3, 8) would give you [3, 4, 5, 6, 7]. range(8, 3), however, will not work, as the start number is greater than the stop number. This means you cannot count down with only 2 arguments.

The third optional argument for range is the step size; how much you want the numbers to increase or decrease by each step. For example, range(0, 10, 2) will give you the output [0, 2, 4, 6, 8], stopping before 10. Here is where you can produce a descending range, by setting the step argument to a negative number. range(10, 0, -2) will give you [10, 8, 6, 4, 2], again stopping before the second argument, and range(10, 0, -1) will give you the full [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].

Finally, the len(iterable) function will give you the length of whatever you give it, or the number of items contained in say a list. For example len("Hello!") will give you 6, and len([1, 2, 3, 4, 5]) will give you 5.

Putting this all together, the line for i in range(len(array)-1, 0, -1) will do the following:

  • the code will repeat as many times as there are items in a list, with i taking on each value in the list
  • that list is a range of numbers
  • that start number of the range is the length of array minus one
  • the end of the range is 0
  • the range is descending, with a step size of -1

Thus if array were ["fish", "banana", "pineapple", "onion"], len(array) will return 4, so you will have for i in range(3, 0, -1), which will loop 3 times, with i being 3, then 2, then 1.

This was a rather simplified answer, so I suggest you find some tutorials on any functions you don’t understand.

Answered By – Isaac Middlemiss

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.