Usage: Perform bubble sort for a list of integers. Click the Next button to move the index to the next position to perform a swap if necessary. Click the Reset button to start over with a new random list of the specified size (min 3 and max 20). The Custom Input button enables you to enter a custom list.
i: 1
needNextPass
k
i
Text-to-Speech Off
A list is filled with random numbers.
  1  def bubbleSort(lst):
  2      needNextPass = True
  3  
  4      k = 1
  5      while k < len(lst) and needNextPass:
  6        # lst may be sorted and next pass not needed
  7        needNextPass = False
  8        for i in range(len(lst) - k)):
  9          if lst[i] > lst[i + 1]:
 10            # Swap lst[i] with lst[i + 1]
 11            lst[i], lst[i + 1] = lst[i + 1], lst[i]
 12            
 13            needNextPass = True # Next pass still needed
 14  
 15        k += 1 # The elements after index k are sorted