Computer Science Question

Sorting

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 4661 Views - Last Post: 30 April 2009 - 06:01 PM

#16 vangogh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-May 08

Re: Computer Science Question

Posted 17 January 2009 - 09:34 AM

Bubblesort is O(n^2) and shouldn't be used for huge lists. Quicksort is O(nlogn) and is easy to understand/use. From what I remember from data structures class, if you're sorting a gigantic list you should look into something like radix sort which is O(n). The basic idea behind least-significant-digit radix sort is this: First you distribute your numbers into ten bins labeled 0...9 based on their rightmost digit. Then you collect them together from left to right, bottom to top and distribute them again using their second rightmost digit. Then collect and distribute them again based on their third rightmost digit. If the largest number you're sorting is only three digits long then you are done, otherwise keep going. Note: I am not sure about the stability of radix sort.
Was This Post Helpful? 0
  • +
  • -

#17 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Computer Science Question

Posted 17 January 2009 - 11:14 AM

View Postvangogh, on 17 Jan, 2009 - 04:34 PM, said:

Note: I am not sure about the stability of radix sort.


Let's just say you don't want to use radix sort if you're working with numbers from an exponential distribution.
Was This Post Helpful? 0
  • +
  • -

#18 numerical_jerome   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: Computer Science Question

Posted 18 January 2009 - 12:39 AM

Quote

Bubblesort is O(n^2) and shouldn't be used for huge lists. Quicksort is O(nlogn) and is easy to understand/use


Quicksort is O(N^2) as well, with an *average* case run time complexity of N lg(N).

though somewhat nit-picky, the distinction is important, for example in the case of a sorted list, and a pivot of the "left most" element, quick-sort will take almost as much time as a selection sort to run, and in a recursive implementation will have a recursion depth of almost the length of the list!

-Jerome
Was This Post Helpful? 0
  • +
  • -

#19 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Computer Science Question

Posted 18 January 2009 - 07:31 AM

You could simply say that the complexity of QuickSort depends on the choice of its pivot-element.
Was This Post Helpful? 0
  • +
  • -

#20 numerical_jerome   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: Computer Science Question

Posted 18 January 2009 - 10:15 PM

Quick sort is O(N^2) no matter where you put the pivot, or even if the pivot is moved each recursion (as mentioned in my first post). The key is not that you are reducing the *worst case* runtime complexity, but enlarging the set of cases that would achieve the *average case* runtime complexity.

I apologize that my post may seem preachy, but an algorithm that is (for a given problem size N) Nlg(N) 999,999 times in 1,000,000 , and N^2 once, is an O(N^2) algorithm.

Quicksort optimization tricks operate in this manner. They reduce the number of cases where the worst case is realized, but do not eliminate them entirely.

-Jerome
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Computer Science Question

Posted 21 January 2009 - 07:39 PM

I did my science fair project on this. Here are my findings:

Radix sort is the most efficient with an O(n).
Quicksort is after Radix Sort (about 4x the # steps of Radix Sort) with an O(n log n)

Mergesort had 125% the # steps as Quicksort also with an O(n log n)

Insertion Sort, Selection Sort & Bubblesort had an O(n^2), with Insertion sort being the more efficient of the 3 followed by Selection Sort followed by Bubblesort. All 3 were less efficient than Mergesort.
Was This Post Helpful? 0
  • +
  • -

#22 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Computer Science Question

Posted 22 January 2009 - 04:08 PM

So what is your conclusion?

Is Radix sort always best?
Was This Post Helpful? 0
  • +
  • -

#23 numerical_jerome   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: Computer Science Question

Posted 23 January 2009 - 05:44 PM

Quote

Is Radix sort always best?


if you are sorting tokens that lend themselves to a radix sort, i.e.fixed precision integers.
Was This Post Helpful? 0
  • +
  • -

#24 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Computer Science Question

Posted 31 March 2009 - 12:15 AM

I know this is old but still important for anyone looking around in the future concerning the generic efficiency of various comparison based sorts.

Refer to these graphs for the time complexity of those popular algorithms which fall under the classification of O(n^2)

Posted Image

And refer to this graph for the time complexity of those popular algorithms which fall under the classification of O(nlog(n))

Posted Image

For both sets of algorithms, the only difference for smaller inputs is the overhead which is generally implementation specific.
The trade offs to be considered when deciding which sorting algorithm to utilize might be:
ease of implementation
ease of maintenance
execution and memory overhead
performance based on input size
Was This Post Helpful? 0
  • +
  • -

#25 boredom   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-January 09

Re: Computer Science Question

Posted 17 April 2009 - 10:05 AM

Obama doesn't know anything about Computers. who so ever reads computers at least knows Bubble Sort, So he replied ''Bubble sort would be the wrong way to go''. trying to say that he is right.

I think Obama should have answered quick sort.
Was This Post Helpful? 0
  • +
  • -

#26 griffinfujioka   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 26-October 08

Re: Computer Science Question

Posted 30 April 2009 - 06:01 PM

That's awesome that he knew that. I just wrote a program to compare the number of operations for 10, 100, 100, etc. random numbers an insertion sort and quick sort took. Of course the quicksort took less operations and was faster, but the lab was great for showing algorithm efficiency.

This post has been edited by griffinfujioka: 30 April 2009 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2