How to write Bubble sort in Bash

Bubble sort is a simple algorithm that basically bubbles up the elements of the array. This means that it traverses the array multiple times and swaps the adjacent elements if they are in the wrong order, as in the following diagram:

Bubble sort Bash Script

Here is a simple bash script for bubble sort algorithm.

#!/bin/bash

echo "enter maximum number"
read n

# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i 

Sample Output from the above script:

$ bash bubblesort.sh
enter maximum number
6
enter Numbers in array:
2
4
7
8
22
3
Numbers in an array are:
2
4
7
8
22
3

Sorted Numbers
2
3
4
7
8
22
Related Post