Skip to content
← Back to glossary

バブルソートBubble sort

Learn in the textbookSearch and Sorting Algorithms

DefinitionBubble sort (バブルソート) compares two neighbouring values, swaps them if they are out of order, and repeats this from one end to the other.

ExplanationWhen sorting into ascending order, each pass carries the largest value to the end, where it stays. The next pass does the same over the range minus that fixed end, and if a whole pass makes no swaps, the data is already sorted. As with selection sort, the number of comparisons grows roughly with the square of the number of items.

AnalogyIt is like people in a row comparing height only with the person next to them, the taller one stepping one place to the right, all the way to the end of the row.

Memory hookA bubble rises: the large values float up to the end one at a time.

Related past-exam questions(1 question)