3

Possible Duplicate:
Fastest sort of fixed length 6 int array

The task is to find a way to sort 8 random numbers with the least number of comparisons (not operations). I expect that I have to use qSort (divide an array by half, sort and then merge and so on.. it must be quicksort i think). For 8 elements number of comparisons is 17, and i have to prove that there is no way to sort random array with 16 (n minus 1) comparisons.

Thanks

Any case, so must be worst also. I'm in first year of studies, so I don't think we have to do something extraordinary (I study math not IT). And kind of sort I use is mergesort! Thanks in advance.

Community
  • 1
  • 1
nakajuice
  • 691
  • 1
  • 11
  • 23

2 Answers2

3

Mergesort/merge-insertion sort will require 16 comparisons for n=8, which is the minimum worst case number of comparisons.

http://oeis.org/A001768 (number of comparisons for mergesort)

http://oeis.org/A036604 (minimum number of comparisons in general)

see also Sorting an array with minimal number of comparisons

EDIT: without assuming "random numbers" are range restricted integers. If you can make assumptions about the range of values, then there are alternatives.

Community
  • 1
  • 1
2

Radix sort requires no comparisons at all :)

  • 1
    Just because it's not comparative, doesn't mean it requires no comparisons. Even a simple x == x is a comparison, comparisons between digits are comparisons... – Luchian Grigore Dec 30 '11 at 06:29
  • @Luchian Grigore: Radix sort, like Counting Sort and Bucket sort , is not a general purpose sort algorithm. It requires additional conditions on the input: you can not sort an arbitrary input like you do with Quick sort, Merge Sort, and other true general purpose sort algorithms. – Massimo Cafaro Dec 30 '11 at 09:17