Sort6.java


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



/******************************************************************************
 *  Compilation:  javac Sort6.java
 *  Execution:    java Sort6 a b c d e f
 *
 *  Reads in six integers and prints them out in sorted order without
 *  using a loop. Uses 12 compare-exchanges.
 *
 *  Solution submitted by Sailesh Dhungana '11.
 *
 ******************************************************************************/

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

        // read in 6 command-line integers to sort
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int d = Integer.parseInt(args[3]);
        int e = Integer.parseInt(args[4]);
        int f = Integer.parseInt(args[5]);
         if (a > b) { int t = b; b = a; a = t; }
         if (c > d) { int t = d; d = c; c = t; }
         if (e > f) { int t = f; f = e; e = t; }
         if (c > e) { int t = e; e = c; c = t; }
         if (a > c) { int t = c; c = a; a = t; }
         if (b > d) { int t = d; d = b; b = t; }
         if (d > f) { int t = f; f = d; d = t; }
         if (b > c) { int t = c; c = b; b = t; }
         if (d > e) { int t = e; e = d; d = t; }
         if (b > d) { int t = d; d = b; b = t; }
         if (c > e) { int t = e; e = c; c = t; }
         if (c > d) { int t = d; d = c; c = t; }
         System.out.println(a + " " + b + " " + c+ " " + d+ " " + e   + " " + f);
     }
 }


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