LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-29-2007, 05:56 AM   #1
Mr_Shameless
Member
 
Registered: Aug 2006
Location: Asia
Distribution: Ubuntu
Posts: 59

Rep: Reputation: 15
About the program that illustrates the Quick Sort method on wikipedia


Here is the link: http://en.wikipedia.org/wiki/Quick_sort

But i post it here for convenience sake:

Code:
void q_sort(int numbers[], int left, int right)
{
        int l_hold=left, r_hold=right;
        int pivot=numbers[left];
        int t;
        while(left<right)
        {
                while((numbers[right]>=pivot) && (left<right))
                        right--;
                if(left!=right)
                {
                        t=numbers[left];
                        numbers[left]=numbers[right];
                        numbers[right]=t;
                        left++;
                }
                while((numbers[left]<=pivot) && (left<right))
                        left++;
                if(left<right)
                {
                        t=numbers[left];
                        numbers[left]=numbers[right];
                        numbers[right]=t;
                        right--;
                }
        }
        numbers[left]=pivot;
        pivot=left;
        left=l_hold;
        right=r_hold;
        if(left<pivot)
                q_sort(numbers, left, pivot-1);
        if(right>pivot)
                q_sort(numbers, pivot+1, right);
}
The 9th line from bottom up,
Code:
numbers[left]=pivot
what is it used for? Is it unnecessary? I dont understand. Please help me figure out. Thank you very much
 
Old 03-29-2007, 06:08 AM   #2
Mr_Shameless
Member
 
Registered: Aug 2006
Location: Asia
Distribution: Ubuntu
Posts: 59

Original Poster
Rep: Reputation: 15
O, btw, these 3 lines:
Code:
pivot=left;
left=l_hold;
right=r_hold;
are they a bit unnecessary too? Why dont we just use q_sort(numbers, l_hold, left-1), say.

Thank you
 
Old 03-29-2007, 10:40 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,944

Rep: Reputation: Disabled
That line happens to work out since the sorted element type is the same as the array boundary indicator, however if the element wasn't int-compatible then it would not work. You can remove it and the sort should still work. I've taken it upon myself to fix up the example so that it will work with other element types, plus added a few "proper" things. Here are the fixes:
Code:
void q_sort(int numbers[], int left, int right)
{
        int l_hold=left, r_hold=right;
        int pivot; //boundary indicator, not a value holder
        int t;
        while(left<right)
        {
                while((left<right) && (numbers[right]>=numbers[left])) //dynamic benchmark value
                        right--;
                if(left!=right)
                {
                        t=numbers[left];
                        numbers[left]=numbers[right];
                        numbers[right]=t;
                        left++;
                }
                while((left<right) && (numbers[left]<=numbers[right])) //dynamic benchmark value
                        left++;
                if(left!=right) //consistency
                {
                        t=numbers[left];
                        numbers[left]=numbers[right];
                        numbers[right]=t;
                        right--;
                }
        }
         /* numbers[left]=pivot; */ //who knows
         pivot=left;
         left=l_hold;
         right=r_hold;
         if(left<pivot)
                 q_sort(numbers, left, pivot-1);
         if(right>pivot)
                 q_sort(numbers, pivot+1, right);
}
Yes, I would go with what you said to remove those three lines. I would have done a lot with it, actually.

The green types should be changed to fit the element type. All others should stay the same except maybe 'int' should be unsigned. Also, the whole thing could use some cleaning up.
ta0kira
 
Old 04-13-2007, 11:32 AM   #4
Mr_Shameless
Member
 
Registered: Aug 2006
Location: Asia
Distribution: Ubuntu
Posts: 59

Original Poster
Rep: Reputation: 15
O, thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 01:17 PM
2 quick things to sort out.... (kernel + resolution) kevingpo Fedora 4 06-10-2005 09:42 PM
a sort, bookmark or quick locate suggestion ledatica LQ Suggestions & Feedback 1 02-03-2005 06:47 AM
PF: Why isn't quick the default method? OlRoy *BSD 2 12-12-2004 06:12 AM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration