Thursday, July 29, 2010

Bubble Sort Shell Script


#!/bin/bash
# SCRIPT: bubblesort.sh
# LOGIC:
# Bubble sort is a simple sorting, it works by repeatedly stepping
# through the list to be sorted, comparing two items at a time and
# swapping them if they are in the wrong order. If you are sorting
# the data in Ascending order, at the end of the first pass, the
# "heaviest" element has move to bottom. In the second pass, the
# comparisons are made till the last but one position and now second
# largest element is placed at the last but one position. And so
# forth.
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}

#You can also use bellow code
#for ((i=0;i<count;i++))
#do
#echo -n " ${ARRAY[i]} "
#done
}

exchange()
{
temp=${ARRAY[$1]}

ARRAY[$1]=${ARRAY[$2]}

ARRAY[$2]=$temp

}

sortnumbers()
{
for (( last=count-1;last>0;last--))
do
for((i=0;i<last;i++))
do
j=$((i+1))
if [ ${ARRAY[i]} -gt ${ARRAY[j]} ]
then

exchange $i $j

fi
done
done
}

#####################################################################
# Variable Initialization #
#####################################################################

echo "Enter Numbers to be Sorted"

read -a ARRAY

count=${#ARRAY[@]}

#####################################################################
# Main Script Starts Here #
#####################################################################

echo "--------------------------------------------------------------"

echo "Numbers Before Sort:"

printnumbers

echo

sortnumbers

echo "Numbers After Sort: "

printnumbers

echo "--------------------------------------------------------------"



OUTPUT:

[root@www blog]# sh bubblesort.sh
Enter Numbers to be Sorted :
78 34 12 98 21 8 36 98 12 88 7 5 61 -12 62 -1 77 -46
------------------------------------------------------
Numbers Before Sort:
78 34 12 98 21 8 36 98 12 88 7 5 61 -12 62 -1 77 -46

Numbers After Sort:
-46 -12 -1 5 7 8 12 12 21 34 36 61 62 77 78 88 98 98
------------------------------------------------------

NOTE: If we complement the if condition in this program, it will give
out the sorted array in descending order.


Method2: Without Using Arrays


#!/bin/bash
# SCRIPT: bubblesort2.sh
# Without using arrays
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
k=1
while [ $k -le $max ]
do
eval echo -n "\$x$k"
echo -n " "
let k++
done
echo
}

#####################################################################
# Variable Initialization #
#####################################################################

echo -n "Enter Total Numbers to be Sorted : "
read max
count=1
while [ $count -le $max ]
do
echo -n "Enter number $count: "
read x$count
let count++
done

#####################################################################
# Main Script Starts Here #
#####################################################################

echo -e "\nElements Before Sort"
printnumbers

for (( last=count-1;last>0;last--))
do
for ((i=1;i<last;i++))
do
j=$((i+1))
eval sval=\$x$i
eval nval=\$x$j

#The eval command evaluates the command line to complete any shell
#substitutions necessary and then executes the command. So $i and $j
#substituted first then $x1 and $x2 evaluated.

if [ $sval -gt $nval ]
then
eval x$i=$nval
eval x$j=$sval
fi
done
done

echo "Elements After Sort: "
printnumbers



OUTPUT:

[root@www shell]# sh bubblesort2.sh
Enter Total Numbers to be Sorted : 6
Enter number 1: 12
Enter number 2: -4
Enter number 3: 6
Enter number 4: -11
Enter number 5: 43
Enter number 6: 9

Elements Before Sort
12 -4 6 -11 43 9
Elements After Sort:
-11 -4 6 9 12 43

14 comments:

  1. I am practicing with you scripts. For learning your scripts are more helpful.
    Thank you.

    Rithika

    ReplyDelete
  2. Thanks for providing the code......

    ReplyDelete
  3. Thank you. Help me too...

    ReplyDelete
  4. thankyou very much

    ReplyDelete
  5. No funciona esta wea,no tienen idea feos culiaos me los paso por el borde de la pichula conchetumare !!! y que wea viva el chuncho, grande la U !!!!
    saludos pa los cauros del duoc uc plaza oeste !! :D !!
    aqui no !!

    ReplyDelete
    Replies
    1. q wea maricon conchetumare q paseo loji ql penca, tay bio lajo ql.

      Delete
  6. el chofer me toca :(
    por lo menos viajo gratis :)
    chofeeeeeeeeeerrr abrame por detroit :$ !!
    kiero un negro que me asote miauuuuu :$ !!
    voi a un asado con lo cauros del taller, no habian choripanes =(

    ReplyDelete
  7. Thanks for the information your article brings.

    ReplyDelete
  8. In the event that you are burnt out on meeting huge amounts of necessities to apply for an advance, this advance is for you as well. Cash Advance Carson

    ReplyDelete