+ 16

when do you choose mergesort in java? when do you choose insertion sort?

I guess I'm looking for examples for these 2 sort types.selection sort seems pretty straightforward. Thanks for any help.

24th Apr 2017, 8:29 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
2 ответов
+ 12
Insertion sort should be used when there are only few items to sort. Internally Java uses insertion sort to sort arrays of primitives with up to 6 values and switches to merge sort for up to 13 values (I hope I remember this correctly). See this for comparison of the algorithm according to values / sorting time and code examples: http://www.titrias.com/sorting-algorithms-comparison-insertion-sort-vs-merge-sort/
24th Apr 2017, 9:12 PM
Tashi N
Tashi N - avatar
+ 4
InsertionSort is slowly because it is check the value one by one. mergeSort is better than insert.
24th Apr 2017, 10:35 PM
Biobii
Biobii - avatar