Sort3.java


Below is the syntax highlighted version of Sort3.java from §1.3 Conditionals and Loops.



/******************************************************************************
 *  Compilation:  javac Sort3.java
 *  Execution:    java Sort3 a b c
 *
 *  Reads in three integers and prints them out in sorted order without
 *  using a loop.
 *
 *  % java Sort3 33 22 18
 *  18 22 33
 *
 *  % java Sort3 43 12 8
 *  8 12 43
 *
 ******************************************************************************/

public class Sort3 {
    public static void main(String[] args) {

        // read in 3 command-line integers to sort
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);

        // 3 compare-exchanges
        if (b < a) { int t = b; b = a; a = t; }
        if (c < b) { int t = c; c = b; b = t; }
        if (b < a) { int t = b; b = a; a = t; }

        // print out results
        System.out.println(a + " " + b + " " + c);
    }

}


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