Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest simple sorting algorithm (except quick sort/merge sort) for 500 numbers?

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.

like image 570
justyy Avatar asked May 02 '14 10:05

justyy


People also ask

What is the fastest type of sorting algorithm?

If you've observed, the time complexity of Quicksort is O(n logn) in the best and average case scenarios and O(n^2) in the worst case. But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

Which is the fastest sorting algorithm after Quicksort?

Merge sort is more efficient and works faster than quick sort in case of larger array size or datasets. Quick sort is more efficient and works faster than merge sort in case of smaller array size or datasets. Sorting method : The quick sort is internal sorting method where the data is sorted in main memory.

What is one of the fastest and simplest sorting algorithms?

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

Which of the following sorting algorithms is the fastest for a very large list?

Quick sort is the fastest, but it is not always O(N*log N), as there are worst cases where it becomes O(N2). Quicksort is probably more effective for datasets that fit in memory.


2 Answers

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.

like image 148
Mohit Jain Avatar answered Nov 15 '22 10:11

Mohit Jain


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

like image 40
Alejandro Avatar answered Nov 15 '22 11:11

Alejandro