Bubble Sort

Introduction

Bubble Sort is the simplest sorting algorithm. It is not suitable for large data sets as its average and worst-case time complexity is quite high.

How does it work?

The algorithm works by repeatedly swapping the adjacent elements if they are in the wrong order.

begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort

Step By Step

Implementation (JavaScript)

function bubbleSort(values) { for (let i = 0; i < values.length - 1; i++) { for(let j = 0; j < values.length - i - 1; j++) { if (values[j] > values[j + 1]) { let temp = values[j]; values[j] = values[j + 1]; values[j + 1] = values; } } } }

Visualization