A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Bubble Sort in Bash page! Here, you'll find the source code for this program as well as a description of how the program works.
#!/bin/bash
ERROR="Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""
function bubble-sort {
new_array=(${array[@]::${#array[@]}-1}) #all elements except the last one
for i in "${!array[@]}"; do
switch=false;
for j in "${!new_array[@]}" ; do
if [ "${array[j]}" -gt "${array[j+1]}" ]; then
aux=${array[j]}
array[j]=${array[j+1]}
array[j+1]=$aux
switch=true;
fi;
done;
if [ $switch = false ]; then
break;
fi;
done;
}
#Validation to fit criteria
if [ "$#" != "1" ]; then echo $ERROR; exit 1; fi; #wrong input
if [[ ! "$1" =~ "," ]]; then echo $ERROR; exit 1; fi; #wrong format
array=($(echo $@ | tr ", " " "))
if [ "${array[0]}" == "" ]; then echo $ERROR; exit 1; fi; #empty input
if [ "${#array[@]}" == "1" ]; then echo $ERROR; exit 1; fi; #not a list
bubble-sort array
arrayString=${array[@]}
echo "${arrayString//" "/", "}"
Bubble Sort in Bash was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.