java - Why bubble sort is taking more time then selection sort -
i trying various scenarios bubble sort , selection sort. know best case bubble sort o(n) if use break statement. lets if not using break statement, there not swaps (as have if condition it), , should take same or less time selection sort.
but strangely taking more time me.
note : have taken same data set(1 900000) sorted. , using sorted data set, none of algorithms have swappings.
please find program below :
package sorting; import java.util.arraylist; import java.util.calendar; import java.util.date; import java.util.list; public class sorting<item extends comparable>//this item var/field can find while creatng object , hence non static { list<item> list=new arraylist<item>(); public static void main(string args[]) { sorting<integer> ss=new sorting<integer>(); system.out.println("adding item logic started : "+calendar.getinstance().gettime()); for(int i=0;i<90000;i++) { ss.list.add(i); } system.out.println("adding item logic ended : "+calendar.getinstance().gettime()); //selection sort started calendar c1=calendar.getinstance(); system.out.println(c1.gettime()); ss.selectionsort(ss.list); calendar c2=calendar.getinstance(); system.out.println(c2.gettime()); system.out.println("selection sort time taken in seconds : "+(c2.gettimeinmillis()-c1.gettimeinmillis())/1000); // system.out.println(ss.list); //bubble sort started ss.list=new arraylist<integer>(); for(int i=0;i<90000;i++) { ss.list.add(i); } calendar c3=calendar.getinstance(); system.out.println(c3.gettime()); ss.bubblesort(ss.list); calendar c4=calendar.getinstance(); system.out.println(c4.gettime()); system.out.println("bubble sort time taken in seconds : "+(c4.gettimeinmillis()-c3.gettimeinmillis())/1000); // system.out.println(ss.list); } void selectionsort(list<integer> list) { for(int i=0;i<list.size();i++) { int target=(integer)list.get(i); int pos=0; for(int j=i+1;j<list.size();j++) {//system.out.println(i+" "+j); if(target>(integer)list.get(j)) { pos=j; target=(integer)list.get(j); } } if(pos!=0) { integer temp=(integer)list.get(i); list.set(i, (integer)list.get(pos)); list.set(pos, temp); } } } void bubblesort(list<integer> list) { for(int i=list.size()-1;i>0;i--) { int status=0; for(int j=0;j<=i-1;j++) { //system.out.println(i+" "+j); if((integer)list.get(j)>(integer)list.get(j+1)) { int temp=(integer)list.get(j+1); list.set(j+1, (integer)list.get(j)); list.set(j, temp); status++; } } //if(status==0)break; } } }
this program 85 percent giving more time bubble sort , double of insertion sort taking.
adding item logic started : fri jun 26 02:47:13 pdt 2015 adding item logic ended : fri jun 26 02:47:13 pdt 2015 fri jun 26 02:47:13 pdt 2015 fri jun 26 02:47:58 pdt 2015 selection sort time taken in seconds : 44 fri jun 26 02:47:58 pdt 2015 fri jun 26 02:48:46 pdt 2015 bubble sort time taken in seconds : 56
well, see in code, number of iterations in both algorithms same
for(int i=0;i<list.size();i++) for(int j=i+1;j<list.size();j++)
would same as
for(int i=list.size()-1;i>0;i--) for(int j=0;j<=i-1;j++)
so difference should rely on what's happening inside each iteration (i taking inner part of loop, other omit).
in bubble sort:
if((integer)list.get(j)>(integer)list.get(j+1)) { int temp=(integer)list.get(j+1); list.set(j+1, (integer)list.get(j)); list.set(j, temp); status++; }
as list sorted, won't inside if, have 2 list.get(something).
in selection sort:
if(target>(integer)list.get(j)) { pos=j; target=(integer)list.get(j); }
but won't inside if, 1 list.get(something).
in short, selection sort doing less operations in each iteration, may makes program run faster.
Comments
Post a Comment