java - Sorting Algorithms Comparison -
java - Sorting Algorithms Comparison -
i have project, need test 3 sorting algorithms , find out 1 of them insertion sort, bubble sort, selection sort. can't reach methods test using stopwatch in java. created 3 arrays.
int[] arr = new int[100000]; //this array sorted int[] randomarr = new int[100000]; //this array random int[] reversearr = new int[100000]; //this array reversed
i tested these algorithms , here results :
- sort1 sorted array took 11 seconds--reversed array took 13 seconds--random array took 24 seconds - sort2 sorted array took 12 second--reversed array took 12 seconds--random array took 10 seconds - sort3 sorted array took 1 millisecond--reversed array took 12 seconds--random array took 4 seconds
i pretty sure sort3 insertion sort because faster other ones in sorted array. confused sort1 , sort2. bubble sort has o(n) in best case, insertion sort have o(n) in best case when check results, insertion sort best case 1 millisecond bubble sort's best case should 1 millisecond too? how can compare them ?
so have work out which means of examining time taken sort various arrays.
here details on each of algorithms (columns best, average, worst, best space);
so can work out 1 selection sort passing sorted lists of varying size algorithms, , seeing how long takes. 1 acts quadratically, selection sort
.
then, decide between bubble sort
, insertion sort
, you'll need read little more how algorithms work. bubble sort
has affectionately named issue; dinosaurs , turtles, is, big elements @ origin dealt with, little elements @ end inefficiently dealt with. seek profiling 2 remaining algorithms (bubble
, insertion
) sorted lists lastly element beingness smallest, , see how do. 1 performs improve insertion sort.
if these tests on arrays of varying sizes, using simple versions of these algorithms, following:
so, based on above info, know sort1 bubble sort, sort3 selection sort, , sort2 insertion sort.
java arrays algorithm sorting
Comments
Post a Comment