LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 07-15-2013, 11:46 AM   #16
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled

Quote:
Originally Posted by konsolebox View Post
Well like I said it would depend on the comparer function. I don't see it essential to consider the original order of two similar values unless the object in which they belong like in a link list of a set of cells within a row would be affected.
The resulting order of equal elements has absolutely nothing to do with the comparator function. The comparator only implies which elements are equal. A stable algorithm can only change order if there's a strict b<a relationship, where a is initially before b. An unstable algorithm is free to change order as long as !(a<b). This is why pivoting is allowed in quicksort.
Quote:
Originally Posted by konsolebox View Post
If that is the case the comparer function would have to consider as well the original order of those two objects within that list. If the object is arranged through sort IDs instead of lists which is rare then the function would also have to consider the ID.
How would the comparator consider the original order without having to modify the element structure to include an index? It's the algorithm that takes care of this by abiding by the criteria I mentioned above.
Quote:
Originally Posted by konsolebox View Post
I don't see it essential to consider the original order of two similar values unless the object in which they belong like in a link list of a set of cells within a row would be affected.
Most data structures don't have a natural ordering that applies to all situations. Not only that, but it's a waste of time to consider every member of a data structure when sorting a list because you're generally only concerned with a few at a time. I almost always find myself sorting a list of data structures based on a subset of the members, rather than using a comparator that compares every last member.

Kevin Barry
 
Old 07-15-2013, 09:13 PM   #17
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by mina86 View Post
Really? No one mentioned that QuickSort is O(n^2) yet?[1] In the worst case it degenerates to select sort. That's why I much prefer Heap Sort (if I don't care if sorting is stable, but even if I do, I would probably just number the elements and sort by double key).
That's why I said, "minimum running time". It is true that in the worst case, quick sort is just select sort run with recursion. It might even have an exponential running time because of the recursion (like with fibonacci numbers) but I can't say for certain.

Of course, this is most unlikely if you choose a random pivot. You should also avoid advancing your pointers if the element's key is equal to the pivot since that can also lead to lop-sided partitioning. (It turns out that it is better to simply exchange elements who's keys are equal to the pivot than to test for the equality).

Jonathon Shewchuck gives a very good lecture on quicksort in this YOU TUBE.

I am also impressed with heapsort. It might not be as fast as quicksort but it still runs in NlogN time and with its in-array sorting and lack of recursion, it makes an ideal candidate in situations where memory is tight.

The irony is that these fancy sorting routines are often at their worst when dealing with already sorted data.

Last edited by psionl0; 07-15-2013 at 09:16 PM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bubble sort without expected output [C] isamuede Programming 6 01-06-2012 03:30 PM
[SOLVED] problem with bubble sort for array of double bvkim Programming 1 05-10-2010 02:05 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM
bubble sorting a linked list - what's wrong with this code ? indian Programming 11 08-06-2006 07:25 AM
Bubble sort? Becks Programming 4 09-12-2003 01:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration