• 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
6 comments | 9:34 AM

#!/bin/bash#!/bin/bash
# SCRIPT: insertionsort.sh
#
# LOGIC: Here, sorting takes place by inserting a particular element
# at the appropriate position, that’s why the name insertion sorting.
# In the First iteration, second element ARRAY[1] is compared with
# the first element ARRAY[0]. In the second iteration third element
# is compared with first and second element. In general, in every
# iteration an element is compared with all the elements before it.
# While comparing if it is found that the element can be inserted at
# a suitable position, then space is created for it by shifting the
# other elements one position up and inserts the desired element at
# the suitable position. This procedure is repeated for all the
# elements in the list.
#
#####################################################################
# Define Functions Here #
#####################################################################

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

sortnumbers()
{
for((i=1;i<count;i++))
do
Temp=${ARRAY[i]}
j=$((i-1))
while [ $Temp -lt ${ARRAY[j]} ]
do
ARRAY[j+1]=${ARRAY[j]}
let j--
if [ $j == -1 ]
then
break
fi
done
ARRAY[j+1]=$Temp
done
}

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

echo "Enter numbers to be sorted"

read -a ARRAY

count=${#ARRAY[@]}

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

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

echo "Numbers Before Sort:"

printnumbers

sortnumbers

echo "Numbers After Sort: "

printnumbers

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



OUTPUT:

[root@www blog]# sh insertionsort.sh
Enter Numbers to be Sorted :
12 76 34 -34 67 9 -56 5 99 -3 17
---------------------------------------------------------------
Numbers Before Sort:
12 76 34 -34 67 9 -56 5 99 -3 17
Numbers After Sort:
-56 -34 -3 5 9 12 17 34 67 76 99
---------------------------------------------------------------

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

6 comments:

  1. thanks a lot . the answers were useful & understandable.

    ReplyDelete
  2. Hi friends, This is Sarah. I am a technology freak. Your technical information is really useful for me. Keep update your blog.
    Regards…
    android training in Chennai

    ReplyDelete
  3. Android is consider the best operating system for any mobile devices except in the international market. Since the invention the software has met several updates till now.Your content is also reminding that to me now. Thanks for sharing this wonderful information in here.


    Android courses in chennai | Android development course in chennai

    ReplyDelete
  4. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Web designing Course in Chennai | Hadoop Training in Chennai

    ReplyDelete
  5. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Android training in Chennai | Android course in Chennai

    ReplyDelete
  6. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/insertion-sorting-program-in-cpp-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete