Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- enum InputType {
- Console, File;
- }
- static int maxArraySize = 100000000;
- static String outputDirectory = "output.txt";
- static Scanner consoleScanner = new Scanner(System.in);
- static String msgEnterInputType = "Выберите способ ввода файл/консоль (ф/к)";
- static String msgEnterFilePath = "Введите путь к файлу";
- static String msgNoSuchFile = "Файл не найден. Попробуйте ещё раз";
- static String msgEnterArray = "Введите элементы массива, разделённые запятыми";
- static String msgEnterArraySize = "Введите размер массива";
- static String msgArraySizeFormatError = "Размер массива должен быть числом от 1 до " + maxArraySize;
- static String msgGetOutputDirectory = "Введите директорию в которую хотите сохранить вывод";
- static String msgNoSuchDirectory = "Указанная директория не найдена, попробуйте ещё раз";
- static InputType getInputType() {
- InputType answer = null;
- boolean isIncorrect = true;
- do {
- System.out.println(msgEnterInputType);
- String inputLine = consoleScanner.nextLine();
- if (inputLine.equalsIgnoreCase("файл") || inputLine.equalsIgnoreCase("ф")) {
- answer = InputType.File;
- isIncorrect = false;
- } else if (inputLine.equalsIgnoreCase("консоль") || inputLine.equalsIgnoreCase("к")) {
- answer = InputType.Console;
- isIncorrect = false;
- }
- } while (isIncorrect);
- return answer;
- }
- static String getInputFilePath() {
- String path;
- boolean isIncorrect = true;
- do {
- System.out.println(msgEnterFilePath);
- path = consoleScanner.nextLine();
- File file = new File(path);
- if (file.isFile() && file.exists()) {
- isIncorrect = false;
- } else {
- System.out.println(msgNoSuchFile);
- }
- } while (isIncorrect);
- return path;
- }
- static int getArraySize() {
- Boolean isIncorrect;
- int size = 0;
- do {
- isIncorrect = false;
- System.out.println(msgEnterArraySize);
- String inputLine = consoleScanner.nextLine();
- try {
- size = Integer.parseInt(inputLine);
- } catch (Exception ex) {
- System.out.println(msgArraySizeFormatError);
- isIncorrect = true;
- }
- if ((size < 1 || size > maxArraySize) && !isIncorrect) {
- System.out.println(msgArraySizeFormatError);
- isIncorrect = true;
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- return size;
- }
- static int[] getArrayFromConsole(int size) {
- int[] inputArray = new int[size];
- System.out.println(msgEnterArray);
- boolean isIncorrect = false;
- do {
- String inputLine = consoleScanner.nextLine();
- String[] splittedString = inputLine.split(",");
- try {
- for (int i = 0; i < inputArray.length; i++) {
- inputArray[i] = Integer.parseInt(splittedString[i]);
- }
- } catch (Exception ex) {
- System.out.println(msgEnterArray);
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputArray;
- }
- static int[] getArrayFromFile(int size) throws FileNotFoundException {
- int[] inputArray = new int[size];
- boolean isIncorrect = false;
- do {
- String filePath = getInputFilePath();
- File file = new File(filePath);
- Scanner fileScanner = new Scanner(file);
- String inputLine = fileScanner.nextLine();
- String[] splittedString = inputLine.split(",");
- try {
- for (int i = 0; i < inputArray.length; i++) {
- inputArray[i] = Integer.parseInt(splittedString[i]);
- }
- } catch (Exception ex) {
- System.out.println(msgEnterArray);
- isIncorrect = true;
- }
- fileScanner.close();
- } while (isIncorrect);
- return inputArray;
- }
- static int[] getArray() throws FileNotFoundException {
- int[] inputArray = null;
- InputType inputType = getInputType();
- int size = getArraySize();
- if (inputType == InputType.Console) {
- inputArray = getArrayFromConsole(size);
- } else if (inputType == InputType.File) {
- inputArray = getArrayFromFile(size);
- }
- return inputArray;
- }
- static void initFileAndConsole() throws IOException {
- File outputFile = new File(outputDirectory);
- if (outputFile.exists()) {
- outputFile.delete();
- }
- outputFile.createNewFile();
- Files.write(Paths.get(outputDirectory), "Сортировка массива по этапам:\n".getBytes(), StandardOpenOption.APPEND);
- System.out.println("Сортировка массива по этапам:");
- }
- static void printToFile(String toPrint) throws IOException {
- Files.write(Paths.get(outputDirectory), toPrint.getBytes(), StandardOpenOption.APPEND);
- }
- static void printToConsole(String toPrint) {
- System.out.print(toPrint);
- }
- public static void printPreSorted(ArrayList<ArrayList<Integer>> toPrint) throws IOException {
- StringBuilder output = new StringBuilder();
- for (int i = 0; i < toPrint.size(); i++) {
- output.append(Arrays.toString(toPrint.get(i).toArray()));
- }
- output.append("\n");
- printToConsole(output.toString());
- printToFile(output.toString());
- }
- public static void printEndInfo() {
- System.out.println("Вывод сохранён по пути " + System.getProperty("user.dir"));
- }
- public static ArrayList<ArrayList<Integer>> splitArray(int[] toSplit) {
- ArrayList<ArrayList<Integer>> listOfParts = new ArrayList<>();
- ArrayList<Integer> part = new ArrayList<>();
- for (int i = 0; i < toSplit.length; i++) {
- if (part.size() == 0 || part.get(part.size() - 1) < toSplit[i]) {
- part.add(toSplit[i]);
- } else {
- listOfParts.add(part);
- part = new ArrayList<>();
- part.add(toSplit[i]);
- }
- }
- listOfParts.add(part);
- return listOfParts;
- }
- public static int[] ConvertListToArray(ArrayList<Integer> toConvert) {
- int[] outputArray = new int[toConvert.size()];
- for (int i = 0; i < toConvert.size(); i++) {
- outputArray[i] = toConvert.get(i);
- return outputArray;
- }
- return outputArray;
- }
- public static int[] mergeSort(int[] toSort) throws IOException {
- ArrayList<ArrayList<Integer>> listOfParts = splitArray(toSort);
- while (listOfParts.size() > 1) {
- printPreSorted(listOfParts);
- ArrayList<Integer> newPart = new ArrayList<>();
- int newPartLength = listOfParts.get(0).size() + listOfParts.get(1).size();
- for (int i = 0; i < newPartLength; i++) {
- if (listOfParts.get(1).size() == 0 || (listOfParts.get(0).size() > 0 && listOfParts.get(0).get(0) < listOfParts.get(1).get(0))) {
- newPart.add(listOfParts.get(0).get(0));
- listOfParts.get(0).remove(0);
- } else {
- newPart.add(listOfParts.get(1).get(0));
- listOfParts.get(1).remove(0);
- }
- }
- listOfParts.remove(0);
- listOfParts.set(0, newPart);
- }
- printPreSorted(listOfParts);
- int[] sortedArray = ConvertListToArray(listOfParts.get(0));
- return sortedArray;
- }
- public static void main(String[] args) throws IOException {
- int[] unSortedArray = getArray();
- initFileAndConsole();
- mergeSort(unSortedArray);
- printEndInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement