Sorting Algorithms in Java
Sorting Algorithms in Java
Sorting Algorithms in Java
1. Bubble Sort :-
Explanation
Implementation
2. Insertion Sort :-
Explanation
3. Selection Sort :-
Explanation
Implementation
Explanation
Explanation
Explanation
Implementation
Performance Comparison :-
Bubble Insertion Selection MergeS QuickSor
time(ns) HeapSort
Sort Sort Sort ort t
First Run 266,089,476 21,973,989 66,603,076 5,511,069 5,283,411 4,156,005
Second
323,692,591 29,138,068 80,963,267 8,075,023 6,420,768 7,060,203
Run
Third Run 303,853,052 21,380,896 91,810,620 7,765,258 8,009,711 7,622,817
Fourth
410,171,593 30,995,411 96,545,412 6,560,722 5,837,317 2,358,377
Run
Fifth Run 315,602,328 26,119,110 95,742,699 5,471,260 14,629,836 3,331,834
Sixth Run 286,841,514 26,789,954 90,266,152 9,898,465 4,671,969 4,401,080
Seventh
384,841,823 18,979,289 72,569,462 5,135,060 10,348,805 4,982,666
Run
13,678,77
Eight Run 393,849,249 34,476,528 107,951,645 8,436,103 10,142,295 2
Ninth
306,140,830 57,831,705 138,244,799 5,154,343 5,654,133 4,663,260
Run
Tenth
306,686,339 34,594,400 89,442,602 5,601,573 4,675,390 3,148,027
Run
We can evidently see that Bubble Sort is the worst when it comes to performance. Avoid using
it in production if you can't guarantee that it'll handle only small collections and it won't stall
the application.
HeapSort and QuickSort are the best performance-wise. Although they're outputting similar
results, QuickSort tends to be a bit better and more consistent