Very quick question. (Sorting)

Started by
12 comments, last by ToohrVyk 16 years, 4 months ago
Quote:Original post by tsukinokaze
I'm no sorting pro, so it's just a suggestion made from theory not praxis.

If it's almost sorted, how about gnome sort isn't that fast for those types of tasks?
The wikipedia claim about the speed of Gnome sort is at best extroadinarily optimistic and bold.

Gnome Sort operates very much like Insertion Sort, except that it involves repeated swapping instead of what I would call a rotate, and it performs more comparisons because it often ignores much of what it has already determined to be sorted.

Here's some numbers to illustrate. This is 100 items, reverse sorted initially, which is the worse case.
Algorithm:       Comparisons:   Item Assignments:Insertion Sort   4950           5049Gnome Sort       9900           9900
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Advertisement
If you pass on the std::list solution, I would recommend a Merge sort, as it's fairly easy to implement. One note to make, however, is that you'll probably have to sap up some memory for the arrays needed for Merge, and the two additional O(n) operations copying the list to an array, and then creating a list from the sorted array.
william bubel
Quote:Original post by iMalc
Gnome Sort operates very much like Insertion Sort, except that it involves repeated swapping instead of what I would call a rotate, and it performs more comparisons because it often ignores much of what it has already determined to be sorted.


The comparison problem is easily ignored simply by replacing the "rotate" part of insertion sort with a "swap" approach but keeping the same comparison code.

As for your "assignment" counting, you made the incorrect assumption that one swap is equivalent to three assignments (for instance, for std::vector, a swap is an order of magnitude faster than even a single assignment). This is precisely the point of gnome sort.
Quote:Original post by Inmate2993
If you pass on the std::list solution, I would recommend a Merge sort, as it's fairly easy to implement. One note to make, however, is that you'll probably have to sap up some memory for the arrays needed for Merge, and the two additional O(n) operations copying the list to an array, and then creating a list from the sorted array.


Merge sort is best applied to lists, anyway, so the array issues are moot.

This topic is closed to new replies.

Advertisement