CJCoding With Joseph
15free / daySign inCreate account

University of Lethbridge - CPSC

Bubble Sort

Sorting Methods

What Is It?

  • Bubble Sort compares pairs of adjacent elements in an array.

  • If an element is larger than the one next to it, they swap places.

  • This process repeats, pass by pass, until the entire array is sorted.

  • In each pass, the biggest element among the unsorted portion moves (or “bubbles”) to its correct position at the end.


Key Points

  • Easy to understand but not the most efficient for large data sets.

  • Worst-case time complexity: O(n²).


Bubble Sort: Step by Step

Idea: In Bubble Sort, we compare pairs of numbers and swap them if they are out of order. After each pass, the largest number among the unplaced elements “bubbles” to its correct position at the end.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 27 more lessons in University of Lethbridge - CPSC.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.