import java.util.ArrayList; import java.util.Scanner; /** * Solves Problem 19.b of Homework 1. Quoting from the homework: *

* Creates a triangle-shaped sorting network (as discussed in * class, using a right-pointing triangle) of a given size. The * program reads a single integer n on standard input, ignoring all * whitespace. The output is the canonical linear representation of * the triangle-shaped sorting network for n inputs. * * @author Ed Ropple */ public class CnetCreateTrisortMain { /** * Takes a single integer from stdin and generates a trisort of the * type explained in class, then outputs it in canonical linear form on * stdout. * * @param args The command line arguments for the program. Are ignored * by this program. */ public static void main(String[] args) { String input = ""; System.out.println("Enter below a single non-negative integer."); System.out.println("Whitespace is ignored, but your number is not"); System.out.println("checked and will generate an exception."); System.out.println("Note that this number is the number of "); System.out.println("comparators; i.e. inputting 6 will create a"); System.out.println("trisort from 0 to 5; for a trisort of comparators"); System.out.println("0 to 9, input 10. (This value is incidentially"); System.out.println("the same as the number of elements being sorted,"); System.out.println("as the requirements state.)"); System.out.println(); Scanner s = new Scanner(System.in); String foo = ""; // read from stdin, emerge once given non-whitespace input (thank you, // trim function, I love you so) while (true) { System.out.print("> "); foo = s.nextLine().trim(); if (foo.length() < 1) continue; else break; } int trisortSize = Integer.parseInt(foo); ArrayList trisortList = new ArrayList(); for (int i = trisortSize - 1; i > -1; i--) { // ArrayList tempTrisortList = new // ArrayList(); for (int j = 0; j < i; j += 1) { trisortList.add(new ComparatorPair(j, j + 1)); // tempTrisortList.add(new ComparatorPair(j, j + 1)); } // System.out.println(tempTrisortList); } System.out.println(); System.out.println(Functions.makeCanonicalLinearOutput(trisortList)); } }