001package algs21; 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac Sort4.java 005 * Execution: java Sort a b c d 006 * 007 * Reads in four integers and prints them out in sorted order without 008 * using a loop. Uses 5 compare-exchanges. 009 * 010 * % java Sort4 33 22 18 66 011 * 18 22 33 66 012 * 013 * % java Sort4 43 22 18 22 014 * 18 22 22 43 015 * 016 * % java Sort4 43 12 8 4 017 * 4 8 12 43 018 * 019 *************************************************************************/ 020 021public class XSort4 { 022 023 public static void main(String[] args) { 024 args = new String[] { "33", "22", "18", "66" }; 025 026 int a = Integer.parseInt(args[0]); 027 int b = Integer.parseInt(args[1]); 028 int c = Integer.parseInt(args[2]); 029 int d = Integer.parseInt(args[3]); 030 if (a > b) { final int t = b; b = a; a = t; } 031 if (c > d) { final int t = d; d = c; c = t; } 032 if (a > c) { final int t = c; c = a; a = t; } 033 if (b > d) { final int t = d; d = b; b = t; } 034 if (b > c) { final int t = c; c = b; b = t; } 035 036 StdOut.println(a + " " + b + " " + c + " " + d); 037 } 038 039}