Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5.  
  6. public class Selection {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         // assign private instance array to an array of 20
  11.         int[] intArray = new int[20];
  12.  
  13.         // reads numbers from file
  14.         try {
  15.             File file = new File("\\Input.txt");
  16.             BufferedReader input = new BufferedReader(new FileReader(file));
  17.             String line = null;
  18.             while ((line = input.readLine()) != null) {
  19.                 try {
  20.                     String[] tokens = line.split(" ");
  21.                     int arrayPosition = 0;
  22.                     for (String t : tokens) {
  23.                         int x = Integer.parseInt(line);
  24.                         intArray[arrayPosition] = x;
  25.                         arrayPosition++;
  26.                     }
  27.                 } catch (Exception e) {
  28.                     System.out.println("File contained a non-integer");
  29.                 }
  30.             }
  31.         } catch (IOException e) {
  32.             System.out.println("File does not exist");
  33.         }
  34.  
  35.         System.out.print("Data: ");
  36.         for (int x : intArray) {
  37.             System.out.print(x + " ");
  38.         }
  39.         System.out.println("\n");
  40.         sort(intArray);
  41.         // read from file and insert into array of 20
  42.         // pass it into sort method
  43.         // print it to console
  44.  
  45.     }
  46.  
  47.     public static void print(int[] intArray, int passNumber) {
  48.         System.out.print("Pass " + passNumber + ": ");
  49.         for (int c : intArray) {
  50.             System.out.print(c + " ");
  51.         }
  52.         System.out.println("\n");
  53.     }
  54.  
  55.     public static void sort(int[] intArray) {
  56.         // blank array
  57.         int[] tempArray;
  58.  
  59.         // starting position
  60.         for (int x = 0; x <= intArray.length - 1; x++) {
  61.             // assign tempArray to a new empty array of length array.length-1
  62.             // resets array to be blank with every loop
  63.             tempArray = new int[intArray.length];
  64.  
  65.             // position of minInt
  66.             int minIntPos = -1;
  67.             int minInt = Integer.MAX_VALUE;
  68.  
  69.             // finds minimum number and sets it to minInt
  70.             for (int y = x; y <= intArray.length - 1; y++) {
  71.                 if (minInt > intArray[y]) {
  72.                     minInt = intArray[y];
  73.                     minIntPos = y;
  74.                 }
  75.  
  76.             }
  77.  
  78.             // insert minInt to position x in the new array
  79.             tempArray[x] = minInt;
  80.             // loop through old array and insert into positions after (excluding
  81.             // position at minInt)
  82.             for (int z = x; z <= intArray.length - 1; z++) {
  83.                 if (z < minIntPos) {
  84.                     int newPos = z + 1;
  85.                     tempArray[newPos] = intArray[z];
  86.                 } else if (z != minIntPos)
  87.                     tempArray[z] = intArray[z];
  88.             }
  89.             // assign array to be new array
  90.             System.arraycopy(tempArray, x, intArray, x, intArray.length - x);
  91.  
  92.             print(intArray, x);
  93.         }
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement