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
---------------------------------------------------------------
Monday, January 3, 2011
Posted by venu k
28 comments | 11:53 AM
Subscribe to:
Post Comments (Atom)
There's no need for command substitution in "local array=( `echo "$@"` )":
ReplyDeletelocal array=( "$@" )
local array=(`echo "$@"`) will not work.
ReplyDeleteInstead use declare -a array=("${@}")
Crazy
ReplyDeleteQsort [string, dynamic pointer array]
ReplyDelete#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);
}
I read this article and its tell me how to maintain script function with easy way thanks for share it best resume template .
ReplyDeletePandith 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:
ReplyDeleteIndian Astrologer in UK | Top Astrology Services in London | Indian Astrologers in London UK
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.
ReplyDeleteHi dude.. This is very interesting for all kinda peoples
ReplyDeleteAstronomy in Chennai
Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.
ReplyDeleteStudy in Germany Consultants in Chennai | Germany Education Consultants in Chennai
Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.
ReplyDeleteDigital Marketing Company in Chennai
Digital Marketing Company in India
I really like your blog! Continue to write more! Very interesting!
ReplyDeleteIts fantastic explanation lot of information gather it...nice article....
ReplyDeleteSEO Company in Chennai
SEO Company in India
Digital Marketing Company in Chennai
Digital Marketing Company in India
Web Development Company in India
Web Design Company in Chennai
I was more than happy to find this web site. I need to thank you for your moment due to this unbelievable read!! I definitely savored every bit of it and I have you book-marked to see new things in your web site.
ReplyDeleteLet's try to play five nights at freddy’s game now.
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteJava Training In Bangalore
Good
ReplyDeletepython training in bangalore
python online training
Good post!
ReplyDeleteTraining in Chennai.
Final year java projects chennai
Big data hadoop training in chennai
Thank you for sharing this type of interview questions
ReplyDeleteIot Training in Bangalore
Iot Online Training
Itil Interview Questions
Salesforce Interview Questions
Msbi Interview questions
Salesforce Interview Questions
C Interview Questions
aws partners in bangalore
aws partners in india
aws service providers in india
aws service providers
I regularly visit your site and find a lot of interesting information. Not only good posts but also great comments. Thank you and look forward to your page growing stronger.
ReplyDeleteBecause of this, take note of that auto title crediting firms are in strict adherence to these laws and controls. 24/7 car title loans near me chicago
ReplyDeleteIt is really amazing...thanks for sharing....provide more useful information...
ReplyDeleteEmbedded System training in Chennai | Embedded system training institute in chennai | PLC Training institute in chennai | IEEE final year projects in chennai | VLSI training institute in chennai
The training conferred by 10Daneces makes one build up his occupation openness. Unix Shell Scripting Training is granted in such a strategy, to the point that the understudies turn out to be in fact sound and that improves their capacity to work with this learning as technocrats.UNIX SHELL SCRIPTING is a script printed for the shell, or charge line indicator of any working plan. The shell is frequently taken as aneasy space particular program dialect. Trademark operations done by shell scripts incorporate record administration, program execution and printing content. 10Daneces gives direction by basically making the understudy work with UNIX SHELL SCRIPTING. The showing system is set in a manner that the learner gets a genuine vibe of the work. The information is prepared in a well ordered methodology so that the understudies can get that into their head and put into practice it when required. They are allowed to make and oversee tables, do scripting and real feel the beat. The mentors at 10Daneces is the most exceptional. They have the direct data of the technique and truly impart that to their understudies. The understudies feel certain that they are at 10Daneces for the instruction.
ReplyDelete10Daneces is the greatest Shell Scripting training institute in Noida with cutting edge foundation and lab offices and the choices of settling on various courses at Noida Location. 10Daneces in Noida gets ready a great many competitors for Shell Scripting at sensible charges that is redone remembering preparing and course content necessity of every participant.
The cash is given as a loan and is specifically saved into your own financial records, after which the loan reserves, in addition to a charge, are removed from your record toward the finish of the credit which more often than not is your next payday.Payday Loans Chicago
ReplyDeletehttps://obirago.blogspot.com/2016/06/profil-member-shojo-complex16.html?showComment=1539157608893#c2681384654568182701
ReplyDeleteWelcome to AsetQQ.com, the Best and Most Trusted Indonesian Online Poker Site. AsQQQ provides 6 games that can be played in 1 User Id only. Online Poker, DominoQQ, Bandar Ceme, Capsa Susun, Live Poker, Ceme Keliling Games, using Indonesian real money
poker online
Choosing an online casino agent is essential if people want to work on online gambling. http://www.dompetcasino.net/ to make the play scene should really be done so that the smoothness in playing gambling can be enjoyed gambling agency sites online casino agency page can also be done with a system of observing recommendations and recommendations from more experienced players.
judi online
happydomino is also the best and trusted Online Domino Poker site in Indonesia which has been proven by reputation and gained the trust of International Online gambling. Both the review of reputable poker sites and the quality of the games we provide do not need to be doubted. By providing the best service and providing assistance that is not complicated and we will provide an explanation that is as easy as possible so that you can easily understand it. We also provide playing tips and tricks so you can easily win in the best online gambling agent games in Indonesia. happydomino is guaranteed to be the fair play 100% online gambling site. There are no Robots in pure games Member vs Member. Our site is also equipped with the most sophisticated security system that is always updated on online gambling issues.
poker online
POKERSEJATI is a Poker site We provide interesting online game variants. You can access all of these games practically using just one account.
situs poker
JudiKartu is the jadwal bola, first trusted online betting agent in Indonesia that provides live baccarat, roulette and sicbo online services. Giving customer satisfaction and serving better than others.
Always determined to be able to serve customers better. Trying to remain a Soccer Agent, Casino Agent, the best and most trusted Online Betting in Indonesia
Jadwal bola
Search engine optimization (SEO) or Indonesian SEO is the process of influencing the visibility of websites or web pages in search engine results - often referred to as "natural", "organic" or "earned" results. In general, the earlier (or rank higher on search results pages), and more often sites appear in the list of search results, the more visitors will receive from search engine users, and these visitors can be converted to customers.
SEO can target various types of searches, including image search, local search, video search, academic search, news search and industry-specific vertical search engines.
Indonesian SEO
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeleteJava interview questions and answers
Java training in Chennai | Java training institute in Chennai | Java course in Chennai
Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore
Java interview questions and answers
Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
ReplyDeleteData Science Training in Chennai | Best Data science Training in Chennai
Data Science training in anna nagar | Data science training in Chennai
Data Science training in chennai | Best Data Science training in chennai
Data science training in Bangalore | Data Science training institute in Bangalore
Data Science training in marathahalli | Data Science training in Bangalore
Data Science interview questions and answers
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.
ReplyDeletebest rpa training in chennai
rpa training in chennai |
rpa online training
rpa course in bangalore
rpa training in pune
rpa interview questions and answers | blueprism interview questions and answers
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
ReplyDeleteangularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs online Training
angularjs Training in marathahalli
Well you use a hard way for publishing, you could find much easier one!
ReplyDeletepython training in chennai
python course in chennai
python training in bangalore