33
Bubble Sort - Typescript
Bubble sort algorithm is simple and easy. In bubble sort every pair of adjacent value is compared and swap if the first value is greater than the second one. By this with every iteration the greatest value goes to the right side making it ascending order.

const bubbleSort = (arr: number[]): number[] => {
const len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
bubbleSort([5, 3, 1, 4, 6])
Output: [1, 3, 4, 5, 6]
Here how the code is working
That’s all for the bubble sort, thanks for reading!
33