1

Why do i get the same time taken for selection and bubble sort. All my sort methods are working properly, but I get exact time taken for bubble and selection sort..

import java.util.Scanner;
public class sorting{
    public static void selectionsort(double[] list, int a, int b, double temp, int length){
        double min;
        int index=0;
        /*for(int p=0;p<list.length;p++){
            System.out.println("input="+list[p]);
        }*/
        int n=0;
        int status=0;
        while(n<length){
            min=list[n];
            for(int j=n;j<(length-1);j++){
                if(list[j+1]<min){
                    min=list[j+1];
                    index=(j+1);
                    status=1;
                }

            }
            if(min!=list[n]){
                temp=list[n];
                //System.out.println("Original val="+temp);
                //System.out.println("Before n="+n);
                list[n]=min;
                //System.out.println("After n="+n);
                //System.out.println("switch val="+list[n]);
                list[index]=temp;
                //System.out.println("new switch val at="+index);
                n++;
                //System.out.println("Updated");
                /*for(int k=0;k<list.length;k++){
                    System.out.println("Output="+list[k]);
                }*/
            }
            else 
                n++;
        }
        //System.out.println("Done selection:");
        /*for(int k=0;k<list.length;k++){
            System.out.println("Output="+list[k]);
        }*/
    }
    public static void bubblesort(double[] list, int a, int b, double temp, double length){
        /*for(int p=0;p<list.length;p++){
            System.out.println("inputb="+list[p]);
        }*/
        while(length>=0){
            for(int k=0;k<=(length-2);k++){
                if(list[k]>list[k+1]){
                    temp=list[k+1];
                    list[k+1]=list[k];
                    list[k]=temp;
                }
            }
            length--;
        }
        //System.out.println("Done bubble:");
        /*for(int k=0;k<list.length;k++){
            System.out.println("Outputb="+list[k]);
        }*/
    }
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        System.out.println("Lower bound");
        int a=in.nextInt();
        System.out.println("High bound");
        int b=in.nextInt();
        System.out.println("how many elements?");
        int n=in.nextInt();
        double[] list=new double[n];
        double[] sel=new double[n];
        double[] bub=new double[n];
        for(int i=0;i<list.length;i++){//intializes the array 
            list[i]=((Math.random()*(b-a)))+a;
        }
        for(int h=0;h<sel.length;h++){//selection
            sel[h]=list[h];
        }
        for(int l=0;l<bub.length;l++){//bubble
            bub[l]=list[l];
        }
        double temp=0.0;
        int length=list.length;
        long startTime = System.nanoTime();
        selectionsort(sel,a,b,temp,length);
        long duration = System.nanoTime() - startTime;
        System.out.println("Time for selection="+(duration*1.0E-9));
        long startTime1 = System.nanoTime();
        bubblesort(bub,a,b,temp,length);
        long duration1 = System.nanoTime() - startTime1;
        System.out.println("Time for bubble="+(duration*1.0E-9));
    }
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    I found your bug... `"Time for bubble="+(duration*1.0E-9)` should be `"Time for bubble="+(duration1*1.0E-9)`. Secondly, use a few million samples – Shark Dec 15 '16 at 17:57
  • 1
    First and foremost, running a benchmark once is unlikely to benchmark anything apart from the JIT. What parameters are you running with? A few million? – Boris the Spider Dec 15 '16 at 17:57
  • You will see the difference of performance speed on a larger sample. see http://stackoverflow.com/questions/10428336/insertion-sort-better-than-bubble-sort for discussion on this. – dreamer Dec 15 '16 at 18:00

1 Answers1

4

Because you're printing duration twice, in the second printing you should use duration1.

Further, as Boris wrote in the comments section: that's not the way to do benchmarking. Better use a benchmarking framework which will take care of warming up the JVM and running the two methods multiple times to find an average running time and etc.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129