Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,412 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,747 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Difference between quicksort and mergesort

 
Reply to this topicStart new topic

Difference between quicksort and mergesort

The Architect 2.0
post 3 Jul, 2008 - 09:09 PM
Post #1


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 43

can someone explain the difference between quicksort and mergesort to me?

they both seem identical, with the only difference between them that mergesort divides exactly at the middle element, whereas quicksort attempts to pick the 'true' median element.

also, from what i've read, it is 'expected' that mergesort keep on dividing till there are n lists of 1 element, whereas quicksort alternatively CAN divide until its sublists reach a certain length(or a certain recursive height has been reached), at which point another [linear(?)] sorting algorithm would be used
User is offlineProfile CardPM

Go to the top of the page


perfectly.insane
post 3 Jul, 2008 - 10:18 PM
Post #2


D.I.C Regular

Group Icon
Joined: 22 Mar, 2008
Posts: 413



Thanked 26 times
My Contributions


QUOTE(The Architect 2.0 @ 3 Jul, 2008 - 09:09 PM) *

can someone explain the difference between quicksort and mergesort to me?

they both seem identical, with the only difference between them that mergesort divides exactly at the middle element, whereas quicksort attempts to pick the 'true' median element.

also, from what i've read, it is 'expected' that mergesort keep on dividing till there are n lists of 1 element, whereas quicksort alternatively CAN divide until its sublists reach a certain length(or a certain recursive height has been reached), at which point another [linear(?)] sorting algorithm would be used


Mergesort is bottom-up, while quick-sort is top-down. Even though you might see the term top-down associated with a particular implementation of mergesort, a merge sort works on that if you have two ordered lists, combining them to get a larger sorted list involves a simple comparison of the head element of each list, until each sublist has been processed. Since a merge requires the lists to be ordered, merges must be applied to sublists of increasing size.

Quicksort is applied on the entire list first, then each sublist (opposite of merge sort). If I have element X, and I place all elements less than X before it, and everything else after, then X will be in it's final position at the end of the sort. After all, nothing on the one side could ever be put on the other side, as they're all less than X.

There's no reason why you couldn't apply a linear sort in combination with a merge sort. It's just that there's little incentive to do so. With quicksort, optimal performance is obtained by selecting the optimal partition element (a.k.a. X). If X is always the least or greatest element in the list, then performance becomes more like O(N**2). There are ways around it, and one of them is the introsort algorithm (a quicksort that changes to a heapsort when the recursion depth exceeds a threshold).
User is offlineProfile CardPM

Go to the top of the page

The Architect 2.0
post 3 Jul, 2008 - 10:35 PM
Post #3


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 43

ah, i understand now.

quicksort does all the comparisons/sorting on the 'top-half'(when lists are beign divided) of the algorithm, whereas mergesort does all the sorting on the 'bottom-half'(when the lists are being merged).

thanks alot.

User is offlineProfile CardPM

Go to the top of the page

ibaraku
post 4 Jul, 2008 - 01:12 PM
Post #4


D.I.C Head

**
Joined: 12 May, 2007
Posts: 132



Thanked 1 times
My Contributions


If you'd like to see the time comparison between merge and quicksort, take a look at this
http://www.only-connect.org/hirowiki/wiki.cgi?SortingClass
quicksort is a bit faster than merge sort when dealing with large data
User is offlineProfile CardPM

Go to the top of the page

KYA
post 4 Jul, 2008 - 05:03 PM
Post #5


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 2,416



Thanked 13 times

Dream Kudos: 1150
My Contributions


I'd also add that, in general, merge sorting requires more memory.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/20/08 05:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month