Y13 Unit 0 - Class Structure
Y13 Unit 1 - Searching Algorithms
Y13 Unit 2 - Abstract Data Structures (HL)
Y13 Unit 3 - Computer Organization (Binary)
Y13 Unit 4 - Computer Organization (Architecture)
Y13 Unit 5 - Resource Management (HL)
Y13 Unit 6 - Control (HL)
Paper 3
1 of 2

Bubble Sort

Sorting is a method for reorganizing a number of items into a desired order. You can sort things alphabetically, from lowest-to-highest or by any other measure desired.

Bubble sort is a simple sort that swaps numbers after comparison. The main difference between bubble and insertion sort is that neighbor elements are compared in bubble sort as opposed to the consistent placing of an element at the beginning of the array.

The name bubble comes from the “bubbling” up of larger numbers as they are compared. You’ll notice this in the visualization below.

You can see a visualization here: https://visualgo.net/en/sorting

Steps

The general outline for bubble sort is:

  • loop through all of the elements (lines 18 and 19)
  • compare adjacent items:
    • if the left element is greater than the right element (line 20)
      • swap the left with the right element(line 22-24)
  • repeat until no swaps are required