Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- class SelectionSort {
- private static final Scanner scanner = new Scanner(System.in);
- static Scanner fileScanner;
- public static void main(String[] args) {
- int[] arr = input();
- arr = sortArrayBySelectionSort(arr);
- output(arr);
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- private static boolean choose() {
- int inputNumber;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- inputNumber = inputData();
- if (inputNumber < MIN_NUM || inputNumber > MAX_NUM) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- private static int inputArraySize() {
- boolean isIncorrect;
- final int MIN_SIZE = 2;
- int num;
- do {
- num = inputData();
- isIncorrect = false;
- if (num < MIN_SIZE) {
- System.out.println("Please, enter a number > 2:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- private static int[] inputArray(int num) {
- int[] arr = new int[num];
- for (int i = 0; i < arr.length; i++)
- arr[i] = inputData();
- return arr;
- }
- private static int[] sortArrayBySelectionSort(int[] arr) {
- System.out.println("Sort stages:");
- int min;
- for (int i = 0; i < arr.length - 1; i++){
- for (int j = i + 1; j < arr.length; j++)
- if (arr[j] < arr[i]) {
- min = arr[j];
- arr[j] = arr[i];
- arr[i] = min;
- }
- System.out.print("Stage " + (i + 1) + ": ");
- for (int j : arr) System.out.print(j + " ");
- System.out.println();
- }
- return arr;
- }
- private static void consoleOutput(int[] arr) {
- for (int j : arr) System.out.print(j + " ");
- System.out.println();
- }
- //files
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static int inputSizeOfArrayFromFile(String path) {
- int num = 0;
- boolean isIncorrect;
- final int MIN = 1;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- num = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file");
- path = inputFilePath();
- }
- if (!isIncorrect && (num < MIN)) {
- isIncorrect = true;
- System.out.println("Matrix size should be at least 2");
- }
- } while (isIncorrect);
- return num;
- }
- private static int[]inputArrayFile(String path, int num) {
- boolean isIncorrect;
- int[] arr = new int[num];
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- for (int i = 0; (i < arr.length); i++) {
- arr[i] = fileScanner.nextInt();
- }
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Reading of array elements failed.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return arr;
- }
- private static void fileOutput(int[] arr, String path) {
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- writer.write("Sorted array: \n");
- for (int j : arr) {
- writer.write(j + " ");
- }
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Mistake of output in file. Input path.");
- path = inputFilePath();
- }
- } while(isIncorrect);
- System.out.println("Successful output in file.");
- }
- private static int[] input () {
- System.out.println("Selection sort. Demonstration.");
- System.out.println("Enter type of input: " + '\n' + "1 is console input, 0 is file input.");
- int num;
- int[] arr;
- String path;
- boolean chose = choose();
- if (chose) {
- System.out.println("Input size of array:");
- num = inputArraySize();
- System.out.println("Input array elements:");
- arr = inputArray(num);
- System.out.println("Unsorted array:");
- consoleOutput(arr);
- } else {
- path = inputFilePath();
- num = inputSizeOfArrayFromFile(path);
- arr = inputArrayFile(path, num);
- }
- return arr;
- }
- private static void output (int[] arr){
- System.out.println("Enter type of output: " + '\n' + "1 is console output, 0 is file output.");
- String path;
- boolean chose = choose();
- if (chose) {
- System.out.println("Sorted array:");
- consoleOutput(arr);
- } else {
- path = inputFilePath();
- fileOutput(arr, path);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement