IntegerSort.java


Below is the syntax highlighted version of IntegerSort.java from §2.3 Quicksort.


/*************************************************************************
 *  Compilation:  javac IntegerSort.java
 *  Execution:    java IntegerSort N < input.txt
 *  Dependencies: StdIn.java
 *
 *************************************************************************/


import java.util.Arrays;

public class IntegerSort {

    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]);

        // initialize and read in data
        Stopwatch timer1 = new Stopwatch();
        int[] a = new int[N];
        for (int i = 0; i < N; i++)
            a[i] = StdIn.readInt();
        double elapsed1 = timer1.elapsedTime();
        System.err.println("Input:  " + elapsed1 + " seconds");

        // sort
        Stopwatch timer2 = new Stopwatch();
        Arrays.sort(a, 0, N);
        double elapsed2 = timer2.elapsedTime();
        System.err.println("Sort:   " + elapsed2 + " seconds");

        // print first 10 values in sorted order
        for (int i = 0; i < N && i < 10; i++)
            StdOut.println(a[i]);
        StdOut.println("...");
    }

}


Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Mon Jul 25 10:29:33 EDT 2011.