Sunday 2 February 2014

Best algorithms for sorting arrays - Java

Bubble sort, Insertion sort and Selection sort



Which one you will use depends on what do you need. Bubble sort is a simplest algorithm, it will do the job but he will be slow, if you need something faster try Selection Sort and Insertion Sort.

They use different methods of sorting if you want to know how they works read it on this links:

My example of implementation this 3 algorithm's, I add speed test so you will see results which one is the best.
Notice: If you are using algorithms for different things, test it in all 3 algorithms it can give different results.


public class Example {

    public static void insertion_sort (int array[]) {
        long start = System.nanoTime();
        for (int i = 1; i < array.length; i++) {
            int j = i;
            int B = array[i];
            while ((j > 0) && (array[j-1] > B)) {
                array[j] = array[j-1];
                j--;
            }
            array[j] = B;
        }
        long total = System.nanoTime() - start;
        System.out.println("Time needed for sorting: "
                + (total / 1000000.0));
    }

    public static void selection_sort (int array[]) {
        long start = System.nanoTime();
        for (int x = 0; x < array.length; x++) {
            int index_of_min = x;
            for (int y = x; y < array.length; y++) {
                if (array[index_of_min] < array[y]) {
                    index_of_min = y;
                }
            }
            int temp = array[x];
            array[x] = array[index_of_min];
            array[index_of_min] = temp;
        }
        long total = System.nanoTime() - start;
        System.out.println("
Time needed for sorting: "
                + (total / 1000000.0));
    }


    public static void bubble_sort( int a[] ) {
        long start = System.nanoTime();
        int i, j,t=0;
        for (i = 0; i < a.length; i++) {
            for (j = 1; j < (a.length-i); j++) {
                if (a[j-1] > a[j]){
                    t = a[j-1];
                    a[j-1] = a[j];
                    a[j] = t;
                }
            }
        }
        long total = System.nanoTime() - start;
        System.out.println("
Time needed for sorting: "
                + (total / 1000000.0));
    }
   
   
    public static void main (String [] args) {
        int i;
        int array[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        bubble_sort(array);
        System.out.print("After bubble sort
algorithm:\n");
        for(i = 0; i <array.length; i++)
        System.out.print(array[i]+"  ");
        System.out.println();
       
        int p;
        int array2[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        selection_sort(array2);
        System.out.print("After selection sort algorithm:\n");
        for(p = 0; p < array2.length; p++)
        System.out.print(array2[p]+"  ");
        System.out.println();
       
        int c;
        int array3[] = {12,9,3,5,7,9,11,17,22,15,4,99,120,1,3,10};
        System.out.println();
        insertion_sort(array3);
        System.out.print("After insertion sort
algorithm:\n");
        for(c = 0; c < array3.length; c++)
        System.out.print(array3[c]+"  ");
        System.out.println();

    }
}


If you see this as useful please subscribe, like it, share it do whatever you want with it.
Have a good one. M.L.


No comments:

Post a Comment