Bubble sort
A bubble sortA sorting algorithm that repeatedly passes through a list to be sorted, comparing and swapping items that are in the wrong order. is the simplest of the sorting algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs..
Bubble sorts work like this:
- Start at the beginning of the list.
- 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.
- Move to the second value in the list. Again, compare this value with the next and swap if the value is bigger.
- Keep going until the there are no more items to compare.
- Go back to the start of the list.
Each run through the list, from start to finish, is known as a passA run through a data set.. 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:
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:
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:
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:
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:
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:
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 pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding. 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
More guides on this topic
- The CPU - Eduqas
- Primary storage - Eduqas
- Secondary storage and embedded systems - Eduqas
- Networks - Eduqas
- Internet and cybersecurity - Eduqas
- Data representation - Eduqas
- Storage and data organisation - Eduqas
- Operating systems - Eduqas
- Principles of programming - Eduqas
- Algorithms - Eduqas
- Software development - Eduqas
- Impacts of digital technology on wider society - Eduqas