SHARE
TWEET

Untitled

a guest Feb 28th, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sort;
  2.  
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8.  
  9. class Main {
  10.  
  11.     private static int[] parseStringToArray(String data) {
  12.         String[] numbers = data.split(" ");
  13.         int[] arr = new int[numbers.length];
  14.         for (int i = 0; i < numbers.length; i++) {
  15.             arr[i] = Integer.parseInt(numbers[i]);
  16.         }
  17.         return arr;
  18.     }
  19.  
  20.     private static int[] readFromFile() throws IOException {
  21.         System.out.println("Enter file name");
  22.         Scanner in = new Scanner(System.in);
  23.         String fileName = in.nextLine();
  24.         in = new Scanner(new FileReader(fileName));
  25.         return parseStringToArray(in.nextLine());
  26.     }
  27.  
  28.     private static int[] readFromConsole() {
  29.         System.out.println("Enter an array");
  30.         Scanner in = new Scanner(System.in);
  31.         return parseStringToArray(in.nextLine());
  32.     }
  33.  
  34.     private static void writeArrayToFile(int[] array) throws IOException {
  35.         StringBuilder outputString = new StringBuilder();
  36.         for (int i = 0; i < array.length; i++) {
  37.             outputString.append(array[i]).append(" ");
  38.         }
  39.  
  40.         System.out.println("Enter file name:");
  41.         Scanner in = new Scanner(System.in);
  42.         String fileName = in.nextLine();
  43.  
  44.         FileWriter out = new FileWriter(fileName, false);
  45.         out.write(outputString.toString());
  46.         out.flush();
  47.         out.close();
  48.  
  49.         System.out.println("Saved to " + fileName);
  50.     }
  51.  
  52.     public static void main(String[] args) throws IOException {
  53.         int[] array;
  54.         System.out.println("Theme: Sort an array using recursive selection sort \nPick an option:\n1 - Read from file\n2 - Read from console");
  55.         Scanner in = new Scanner(System.in);
  56.         int option = in.nextInt();
  57.         in.nextLine();
  58.  
  59.         switch (option) {
  60.             case 1:
  61.                 array = readFromFile();
  62.                 break;
  63.             case 2:
  64.                 array = readFromConsole();
  65.                 break;
  66.             default:
  67.                 array = new int[0];
  68.                 break;
  69.         }
  70.  
  71.         System.out.println("Initial array: " + Arrays.toString(array));
  72.         recursiveSort(array, array.length, 0);
  73.         System.out.println("Result: " + Arrays.toString(array));
  74.         System.out.println("Save the result to a file?\n1 - Yes\n2 - No");
  75.         option = in.nextInt();
  76.  
  77.         if (option == 1) {
  78.             writeArrayToFile(array);
  79.         } else {
  80.             System.out.println("File not saved");
  81.         }
  82.     }
  83.  
  84.  
  85.     static int minIndex(int[] array, int i, int j)
  86.     {
  87.         if (i == j)
  88.             return i;
  89.         int k = minIndex(array, i + 1, j);
  90.         return (array[i] < array[k])? i : k;
  91.     }
  92.  
  93.     static void recursiveSort(int[] array, int n, int index)
  94.     {
  95.         if (index == n)
  96.             return;
  97.         int k = minIndex(array, index, n-1);
  98.         if (k != index) {
  99.             int temp = array[k];
  100.             array[k] = array[index];
  101.             array[index] = temp;
  102.         }
  103.         recursiveSort(array, n, index + 1);
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top