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    void bubbleSort(int list[], int arraySize)
  2    {
  3      bool needNextPass = true;
  4      
  5      for (int k = 1; k < arraySize && needNextPass; k++)
  6      {
  7        // Array may be sorted and next pass not needed
  8        needNextPass = false;
  9        for (int i = 0; i < arraySize - k; i++)
 10        {
 11          if (list[i] > list[i + 1]) {
 12            // Swap list[i] with list[i + 1]
 13            int temp = list[i];
 14            list[i] = list[i + 1];
 15            list[i + 1] = temp;
 16            
 17            needNextPass = true; // Next pass still needed
 18          }
 19        }
 20      }
 21    }