Which sorting algorithm is best suited to re-sort an almost fully sorted list?

后端 未结 4 1318
再見小時候
再見小時候 2020-12-17 21:42

I have a list of strings which has been sorted by a specific comparison function.

Now I have to re-sort this list using a different comparison function.

相关标签:
4条回答
  • 2020-12-17 22:14

    Insertion sort works well on small or nearly sorted lists.

    From this ACM Paper:

    Tests on randomly generated lists of various combinations of list length and small sortedness ratios indicate that Straight Insertion Sort is best for small or very nearly sorted lists and that Quickersort is best otherwise.

    From wiki article Insertion sort:

    If the input array is already sorted, insertion sort performs as few as n-1 comparisons, thus making insertion sort more efficient when given sorted or "nearly-sorted" arrays.

    SO Question: Is there ever a good reason to use Insertion Sort?

    0 讨论(0)
  • 2020-12-17 22:15

    As I understood your data list is allready sorted (let say by ascii/country charset order), but without some dictionary rules applied for particular country. For example Germany and their Umlauts

    see Germanic_umlaut in wikipedia

    you are not inserting new items, you just want to resort them by little bit more strict sorting rule.

    as you can read for example here

    http://www.softpanorama.org/Algorithms/Sorting/bubblesort.shtml

    bubble sort works well on allready sorted lists with just a few permutations. This sounds like bubble sort is good algorithm to start with. Also note that bubble sort is "stable" sorting algorithm. This could be important for your scenario.

    0 讨论(0)
  • 2020-12-17 22:17

    For nearly sorted lists, variations of the Comb sort outperform the Quicksort. I haven't tested to see how the comb sort compares to the Insertion sort.

    0 讨论(0)
  • 2020-12-17 22:19

    Have access to both search operations? If yes, you can to build some hash tree during first sorting process and use it to other sort operations

    0 讨论(0)
提交回复
热议问题