• 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.

Monday, January 3, 2011

Posted by venu k
10 comments | 11:53 AM
   
Quicksort is a good example of the divide and conquer strategy for
solving problems. In quicksort, we divide the array of items to be
sorted into two partitions and then call the quicksort procedure recu-
rsively to sort the two partitions, ie we divide the problem into two
smaller ones and conquer by solving the smaller ones

Quicksort recursive algorithm:

1 Select an element (called as pivot) x(p) of x .
2 Divide x into two batches x1 and x2 so that
all entries of x1 are < x(p) and
all entries of x2 are > x(p).
all entries of x3 are == x(p)
for each recursive call x3 will be placed to its sorted position.
To finish sorting we must sort x1 and x2.
3 Apply steps 1 and 2 again to each of x1
and x2, using further subdivision.
4 Repeat (recursively) until the sets to be
sorted have no more than one element.

#!/bin/bash
# SCRIPT : quicksort.sh
# USAGE : quicksort.sh
# PURPOSE: Sorts the list using quicksort algorithm.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Define Functions Here #
#####################################################################

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

sortnumbers()
{

local array=( `echo "$@"` )
local -a l
local -a g
local -a e
local x=

if [ ${#array[@]} -lt 2 ]; then
echo -n ${array[@]}
else
local pivot=${array[0]}

for x in ${array[@]}
do

if [ $x -lt $pivot ]
then
l=( ${l[@]} $x )
elif [ $x -gt $pivot ]
then
g=( ${g[@]} $x )
else
e=(${e[@]} $x)
fi

done

echo "`sortnumbers "${l[@]}"` ${e[@]} `sortnumbers "${g[@]}"`"

fi
}

#####################################################################
# Variable Declaration #
#####################################################################

clear

echo "Enter Numbers to be Sorted : "

read -a ARRAY

count=${#ARRAY[@]}

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

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

echo "Numbers Before Sort:"

printnumbers

echo "Numbers After Sort: "

sortnumbers "${ARRAY[@]}"

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


OUTPUT:

[venu@localhost shell]$ sh quicksort.sh
Enter Numbers to be Sorted :
12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9
---------------------------------------------------------------
Numbers Before Sort:
12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9
Numbers After Sort:
-46 -11 -8 0 5 9 12 21 32 32 33 54 54 76 84 87 90 222 321
---------------------------------------------------------------

10 comments:

  1. There's no need for command substitution in "local array=( `echo "$@"` )":

    local array=( "$@" )

    ReplyDelete
  2. local array=(`echo "$@"`) will not work.

    Instead use declare -a array=("${@}")

    ReplyDelete
  3. Qsort [string, dynamic pointer array]

    #include
    #include
    #include

    void sortstrarr(void *array, unsigned n);
    static int cmpr(const void *a, const void *b);

    int main (void) {
    char **strarray = NULL;
    int i = 0, strcount = 0;
    char line[1024];

    while((fgets(line, 1024, stdin)) != NULL) {
    if(strlen(line) == 1)
    continue;

    strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
    strarray[strcount++] = strdup(line);
    }

    printf("### Before ###\n");
    for(i = 0; i < strcount; i++)
    printf("%2d: %s", i, strarray[i]);

    sortstrarr(strarray, strcount);

    printf("### After ###\n");
    for(i = 0; i < strcount; i++)
    printf("%2d: %s", i, strarray[i]);

    /* free mem... */
    for(i = 0; i < strcount; i++)
    free(strarray[i]);

    free(strarray);
    return 0;
    }

    static int cmpr(const void *a, const void *b) {
    return strcmp(*(char **)a, *(char **)b);
    }

    void sortstrarr(void *array, unsigned n) {
    qsort(array, n, sizeof(char *), cmpr);
    }

    ReplyDelete
  4. I read this article and its tell me how to maintain script function with easy way thanks for share it best resume template .

    ReplyDelete
  5. Thanks for sharing this nice post. Idioms can have a literal meaning in one situation and a different idiomatic meaning in another situation. German classes in Chennai | German language classes in Chennai | German Courses in Chennai

    ReplyDelete
  6. Now days, Learning French language is in very demand. People want to get job in France. French Classes in Chennai | French Training in Chennai

    ReplyDelete
  7. Pandith Raghavendra ji world No 1 Gold Medalist Astrologer - World Famous Astrologer in London, and he always give and more solution for astrology issue, if you have problem kindly look at:

    Indian Astrologer in UK | Top Astrology Services in London | Indian Astrologers in London UK

    ReplyDelete
  8. Thank you very much for sharing. It was very kind of you. company with the great experience in writing If you wonder what that is, it is an article I would want you to look through in case you are interested and eager to find out new things.

    ReplyDelete
  9. Hi dude.. This is very interesting for all kinda peoples


    Astronomy in Chennai

    ReplyDelete