Here is my code - a bubble sort algorithm for sorting list elements in asc order:
foo = [7, 0, 3, 4, -1]
cnt = 0
for i in foo:
for i in range(len(foo)-1):
if foo[cnt] > foo[cnt + 1]:
temp = foo[cnt]
c[cnt] = c[cnt + 1]
c[cnt + 1] = temp
cnt = cnt + 1
cnt = 0
I've been revising my code, but it is still too inefficient for an online judge. Some help would be greatly appreciated!
count
by usingenumerate
a, b = b, a
.changed
.i
elements will have been sorted, so you can further reduce the constant factor associated with the algorithm.Note that none of these optimisations change the asymptotic (Big-O) complexity of BubbleSort (which remains
O(N ** 2)
), instead, only reduces the constant factors associated.One easy optimization is to start second loop from i+1 index:
Since you already sorted everything up to index i there is no need to iterate over it again. This can save you more than 50% of comparisons - in this case it's 10 versus 25 in your original algorithm.
You need to understand the big Oh notation in order to understand how efficient your algorithm is in terms of usage of computational resources independent of computer architecture or clock rate. It basically helps you analyze the worst case running time or memory usage of your algorithm as the size of the input increases. In summary, the running time of your algorithm will fall into one of these categories (from fastest to slowest);
O(1): Constant time. Pronounced (Oh of 1). The fastest time.
O(lg n): Logarithmic time. Pronounced (Oh of log n). Faster than linear time. Traditionally, it is the fastest time bound for search.
O(n): Linear time. Pronounced (Oh of n, n is the size of your input e.g size of an array). Usually something when you need to examine every single bit of your input.
O(nlgn): The fastest time we can currently achieve when performing a sort on a list of elements.
O(n**2): Oh of n squared. Quadratic time. Often this is the bound when we have nested loops.
O(2**n): Really, REALLY big! A number raised to the power of n is slower than n raised to any power.
In your case, you are using nested loops which is O(n2). The code i have written uses a single while loop and has a growth complexity of O(n) which is faster than O(n2). I haven't really tried it on a very large
array
but in your case it seems to work. Try it and let me know if it works as expected.Note: In the example list (k), we only need to loop through the list three times in order for it to be sorted in ascending order. So if you change the while loop to this line of code
while count < 4:
, it would still work.