Skip Top Navigation Bar

Comparing Sorting Algorithms

Algorithm Strategy Best Use
Insertion Sort Insert each element into an already sorted portion of the array. Small or nearly sorted collections.
Selection Sort Repeatedly select the smallest remaining element. Learning basic sorting concepts.
Merge Sort Divide the array into smaller parts, then merge them back together in order. Large collections where efficient sorting is important.

Which Algorithm Should You Use?

Each algorithm has strengths and weaknesses.

For most Java programs, you do not need to implement these algorithms yourself. The Arrays.sort() method uses highly optimized sorting algorithms behind the scenes.