Linearithmic.java


Below is the syntax highlighted version of Linearithmic.java from §4.1 Performance.


/******************************************************************************
 *  Compilation:  javac Linearithmic.java
 *  Execution:    java Linearithmic n
 *
 *  A program with linear running time.
 *
 ******************************************************************************/

public class Linearithmic {

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

        int count = 0;
        for (int i = 1; i <= n; i = i*2)
            for (int j = 0; j < n; j++)
                count++;
        StdOut.println(count);


    }

}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:28:22 EDT 2022.