大象传媒

Sorting, searching and validation - EduqasBubble sort

Sorting and searching are two of the most frequently needed tasks in program design. Common algorithms have evolved to take account of this need, such as linear search, binary search, bubble sort and merge sort.

Part of Computer ScienceUnderstanding Computer Science

Bubble sort

A is the simplest of the sorting .

Sorting a list using the bubble sort algorithm

Bubble sorts work like this:

  1. Start at the beginning of the list.
  2. Compare the first value in the list with the next one up. If the first value is bigger, swap the positions of the two values.
  3. Move to the second value in the list. Again, compare this value with the next and swap if the value is bigger.
  4. Keep going until the there are no more items to compare.
  5. Go back to the start of the list.

Each run through the list, from start to finish, is known as a . The bubble sort continues until a pass is made where no values have been swapped. At this point, the list is sorted.

Consider this unsorted list:

Table with a list of unsorted numbers

The value at position 0 is 9, and the value at position 1 is 4. 9 is bigger than 4, so the two items would be swapped. The list would now be:

Table with a list of unsorted numbers, the numbers at positions zero and one have been swapped

We move up to the next position, position 1. The value at position 1 is 9, and the value at position 2 is 2. 9 is bigger than 2, so the two items would be swapped. The list would now be:

Table with a list of unsorted numbers, the numbers at positions one and two have been swapped

We move up to the next position, position 2. The value at position 2 is 9, and the value at position 3 is 6. 9 is bigger than 6, so the two items would be swapped. The list would now be:

Table with a list of unsorted numbers, the numbers at positions two and three have been swapped

We move up to the next position, position 3. The value at position 3 is 9, and the value at position 4 is 5. 9 is bigger than 5, so the two items would be swapped. The list would now be:

Table with a list of unsorted numbers, the numbers at positions three and four have been swapped

The first pass is now complete. However, this list may still be unsorted, so another pass takes place.

After the second pass, the list would be:

Table with a list of sorted numbers

The second pass is now complete. However, this list may still be unsorted, so another pass takes place.

After the third pass, the list would be the same, as all items were in order after the second pass. However, a bubble sort continues until no swaps are made in a pass. During the third pass, no swaps occurred so now the sort knows that all items are in order.

A algorithm for a bubble sort might be:

counter as integer
                    swapped as Boolean
                    swaps as integer

                    temp as integer
                    list[5]
                    set counter = 0
                    set swapped = TRUE
                    set swaps = 0
                    while swapped = TRUE
                        while counter < len(list)-1
                            if list[counter] > list[counter+1]
                                set temp = list[counter]
                                set list[counter] = list[counter+1]
                                set list[counter+1] = temp
                                set swaps = swaps + 1
                            end if
                            set counter = counter + 1
                        end while
                        if swaps = 0
                            set swapped = FALSE
                        else
                            set swaps = 0
                            set counter = 0
                        end if
                    end while