• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Thursday, July 29, 2010

Posted by venu k
12 comments | 9:23 AM

#!/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

12 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