Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Tuesday, February 24, 2015

Bubble Sort Using Unix Shell Script

This script will sort the given input passed from command line based on bubble sort technique. You can give any number of inputs but only integers.

echo "Enter Values That You Want To Sort(eg. 3 2 1):"
read vals

set -- $vals
n=$#

k=0
for val in $* 
do
    a[$k]=$val
    ((k++))
done

flag=1

for (( i=0; i<$n-1 && $flag==1; i++ ))
do
    flag=0
    for (( j=0; j<$n-i-1; j++ ))
    do
        if [ ${a[$j]} -gt ${a[$j+1]} ]
        then
            temp=${a[$j]}
            a[$j]=${a[$j+1]}
            a[$j+1]=$temp
            flag=1
        fi
    done
done

echo "Sorted: "
for (( l=0; l<$n; l++))
do
    echo -ne "${a[$l]} "
done

echo


$ chmod +x bubble.sh
$ ./bubble.sh
Enter Values To Sort (eg. 3 2 1):
5 4 3 2 1 1 2 3 4 5
Sorted: 
1 1 2 2 3 3 4 4 5 5