Is this a correct way to implement bubble sort?
<p>Is this a correct way to implement bubble sort?
I get a sorted list, but I have a doubt that the method is correct.</p>

<pre><code># Input unsorted list
size_lis = int(input("enter the size of the list"))
size = 0
list1 = list()

while (size &lt; size_lis):
    element = int(input("enter the element"))
    list1.append(element)
    size += 1

# Sort
for i in range(0, len(list1)):
    for j in range(0, len(list1)-1):
        if list1[j] &gt; list1[j+1]:
            list1[j],list1[j+1] = list1[j+1],list1[j]

print(list1)
</code></pre>

<p>In bubble sort the largest element is moved step by step to the end of list. Thus after first pass there is this one element in its final position. The second pass should sort only N-1 remaining elements, etc.</p>

<p>In the posted code, just adjust the inner circle like this. That'll save almost 50% of CPU time.</p>

<pre><code>n = len(lst)
for i in range(n):
    for j in range(n-i-1):
        if lst[j] &gt; lst[j+1]:
            lst[j], lst[j+1] = lst[j+1],lst[j]
</code></pre>

python bubble-sort