Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

code for insertion sort in java

Can anyone provide me with the code and step by step explanation for insertion sort in Java?
0
yattias
Asked:
yattias
3 Solutions
 
yattiasAuthor Commented:
sorry!  I meant selection sort
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
jagadeesh_motamarriCommented:

/*************************************************************************
 *  Compilation:  javac InsertionSort.java
 *  Execution:    java InsertionSort N
 *  
 *  Generate N random numbers between 0 and 1 and insertion sort them.
 *
 *************************************************************************/

public class InsertionSort {
    static long comparisons = 0;
    static long exchanges   = 0;

    public static void insertionSort(double[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++)
            for (int j = i; j > 0; j--)
                if (less(a[j], a[j-1]))
                    exch(a, j, j-1);
                else break;
    }

    // is x < y?
    private static boolean less(double x, double y) {
        comparisons++;
        return (x < y);
    }

    // exchange a[i] and a[j]
    private static void exch(double[] a, int i, int j) {
        exchanges++;
        double swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }



    // generate N real numbers between 0 and 1, and insertion sort them
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        double[] a = new double[N];
        long start, stop;
        double elapsed;


        // read first N strings from stdin
        start = System.currentTimeMillis();
        for (int i = 0; i < N; i++)
            a[i] = Math.random();
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Generating input:  " + elapsed + " seconds");

        // sort them
        start = System.currentTimeMillis();
        insertionSort(a);
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Insertion sort: " + elapsed + " seconds");
        System.out.println("Comparisons: " + comparisons);
        System.out.println("Exchanges:   " + exchanges);
    }
}

......Jags

0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ah, you shouldn't post full-code for questions which look like homework.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now