Last answered:

31 Jan 2022

Posted on:

26 Jan 2022

1

Code snippet for Bubble sort

Took 0 and (-i)  from the range function in the second for loop and the code worked.
I want to understand why need to put to 0 and -i in the range function if the code works without it
Below are snippet of the codes

imageimage

1 answers ( 0 marked as helpful)
Instructor
Posted on:

31 Jan 2022

0

Hey Sylvester,

Thank you for your question!

If no starting value is specified inside range(), the function automatically assumes that we start from 0. Therefore, range(0, 10) and range(10) will give the same output. You can therefore safely remove the 0 argument.

Regarding your second question about the iterator i - it will absolutely work without it. However, by removing it, the algorithm is doing more work than necessary. As the larger numbers are getting sorted at the end of the list, j doesn't need to go all the way to the end every time i increases.

Hope this helps!

Kind regards,
365 Hristina

Submit an answer