6

I would like to implement some sorting algorithm that is fast enough to sort 500 numbers many many times (e.g. 300 iterations of sorting 500 numbers each)

I don't like quick sort, merge sort because they are harder to implement than bubble sort, selection sort, insertion sort..

Just wondering what is the best (simple to implement and with some best cases complexity less than O(N2) if many numbers are already sorted) simplest sorting algorithm in this case

The numbers to sort are type of doubles.

Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
doctorlai
  • 5,223
  • 3
  • 31
  • 66
  • 2
    Probably a derivative of counting sort or bucket sort would suit your needs since you are only sorting numbers. You can even go to radix sort if you want finesse ;) – Alejandro May 02 '14 at 10:05
  • 1
    You would need to specify what you mean by "best" when it comes to a sorting algorithm. If you mean efficient, then there are no algorithms that are both most efficient and simplest. – Guffa May 02 '14 at 10:11
  • 1
    There is no complexity O(2), as that would be O(1), and there can't be a sorting algorithm with less than O(1). Do you mean O(n²)? – Guffa May 02 '14 at 10:18
  • Yes, my mistake, what I meant is that the complexity sometimes is less than O(n^2) if many numbers are already sorted. – doctorlai May 02 '14 at 10:19
  • 2
    Which language are you programming in? Most probably there is already a good sorting algorithm built-in in its library. – Abhishek Bansal May 02 '14 at 10:50
  • I don't think that quick sort is really difficult to implement, but if you don't like it, heap sort is one of the easiest to implement and is as fast as quick/merge sort. – Topo May 02 '14 at 17:31
  • @Topo: It _grows_ as fast as quick/merge sort. It usually executes slower in practice, _especially_ with a pre-sorted list. – Mooing Duck May 02 '14 at 17:48
  • @DoctorLai: With a partially sorted list I'd try a TimSort, which is a mergesort variant optimized for partially sorted lists. – Mooing Duck May 02 '14 at 17:49
  • Actually, considering it's 500 integers, I'd probably do a bucket variant: count how many in each bucket, shuffle them into the right buckets, then insert sort each bucket. That's roughly `N*2 + (N/b)^2` operations where `b` is the number of buckets. – Mooing Duck May 02 '14 at 17:52
  • @MooingDuck I think that quicksort is slower in partially sorted lists because quick sort worst case is when the list is already sorted. – Topo May 02 '14 at 18:10
  • @Topo: That depends entirely on how the partition is picked. If it picks the middle as a partition, then a sorted list becomes quicksorts _best case_. Even for a partially sorted list, such a quicksort performs quite well. (MSVC++ does this) – Mooing Duck May 02 '14 at 18:19

2 Answers2

4

I once compared some sorting algorithms. I found comb sort and heap sort are very easy to implement and give very good results.

void comb_sort(double *a, int size) {
    int gap = size;
    bool swapped = false;
     while ((gap > 1) || swapped) {
        if (gap > 1) gap = int(gap/1.247330950103979);
        swapped = false;
        for (int i = 0; gap + i < size; i++)
        if (a[i + gap] < a[i]) {
           swap(&a[i + gap], &a[i]);
           swapped = true;
        }
    }
}

You can profile multiple algorithms on your data set and choose the best one for your need.

EDIT Adding calculation of magic number I used for comb sort. I found this in some book (that I don't remember any more) years ago.

Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
  • @j_random_hacker I don't remember exactly the complete proof/calculation (I looked back into my books today but could not find where I read this). I noted down only the final result/number best suited for comb sort which comes to 1/(1-pow(e, -1 * [golden_ratio](http://en.wikipedia.org/wiki/Golden_ratio)) and this is the magic number I used. I did some profiling with this factor, and found it effective. I studied it long back, and noted down in my code recipes. If I can find it back, I will post it here. OP is looking for best case complexity, which is linear. Worst case is quadratic. – Mohit Jain May 03 '14 at 02:53
1

You could use radix sort, Which is O(kn) where k is a constant bounding the size of your data with respect to your input size in an O(n^k) manner. Although this sort is usually done with integers, a slight modification allows it to be used for doubles, as mentioned in this stackoverflow post:

Radix sort for doubles

Community
  • 1
  • 1
Alejandro
  • 2,912
  • 1
  • 19
  • 29