Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.Scanner;
- import java.util.regex.PatternSyntaxException;
- public class lab3_3 {
- static Scanner scan = new Scanner(System.in);
- public static void main(String[] args){
- System.out.print("Welcome to the program that will sort an array using cocktail sort\nSelect the source for entering the sequence of numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
- byte inputSource = takeSource();
- int[] array = takeArray(inputSource);
- int[][] answer = findStagesOfCocktailSort(array);
- System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
- byte outputSource = takeSource();
- output(outputSource,answer);
- scan.close();
- }
- static int takeInt(int min,int max) {
- int number = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- number = Integer.parseInt(scan.nextLine());
- } catch (NumberFormatException e) {
- System.out.println("Incorrect input!!!");
- isIncorrect = true;
- }
- if (!isIncorrect && (number < min || number > max)){
- System.out.println("Incorrect input!!!");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return number;
- }
- static byte takeSource(){
- final byte CONSOLE = 1;
- final byte FILE = 2;
- boolean isIncorrect;
- byte choice = 0;
- do {
- isIncorrect = false;
- try {
- choice = Byte.parseByte(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- static String takePathForInput(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (Files.notExists(Path.of(path))) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- static String takePathForOutput(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- static int[] takeArrayFromConsole() {
- final int MIN_SIZE = 2;
- final int MAX_SIZE = 200;
- final int MIN_ELEMENT = -1000;
- final int MAX_ELEMENT = 1000;
- System.out.print("Enter size: ");
- int size = takeInt(MIN_SIZE, MAX_SIZE);
- int[] arr = new int[size];
- for (int i = 0; i < size; i++){
- System.out.print("Enter element " + (i + 1) + ": ");
- arr[i] = takeInt(MIN_ELEMENT, MAX_ELEMENT);
- }
- return arr;
- }
- static int[] takeArrayFromFile(final String path){
- int size = 0;
- final int MIN_SIZE = 2;
- int lineCounter = 0;
- final int SIZE_LINE_NUMBER = 1;
- final int ARRAY_LINE_NUMBER = 2;
- String line;
- String[] stringArray = null;
- boolean isCorrect = true;
- try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
- while ((isCorrect) && ((line = reader.readLine()) != null)) {
- lineCounter++;
- if (lineCounter == SIZE_LINE_NUMBER){
- try {
- size = Integer.parseInt(line);
- } catch (Exception e) {
- System.out.println("Incorrect file content!!! Incorrect information in the size line!!!");
- isCorrect = false;
- }
- if (isCorrect && (size < MIN_SIZE)) {
- System.out.println("Incorrect file content!!! The size must be higher than 1!!!");
- isCorrect = false;
- }
- }
- if (lineCounter == ARRAY_LINE_NUMBER){
- try {
- stringArray = line.split(" ");
- } catch (PatternSyntaxException e){
- System.out.println("Incorrect file content!!! Incorrect information in the array line!!!");
- isCorrect = false;
- }
- }
- }
- }catch (IOException e) {
- System.out.println("Input/Output error!!!");
- isCorrect = false;
- }
- int[] arrayForReturn = null;
- if (stringArray != null) {
- if ((isCorrect) && ((stringArray.length != size) || (lineCounter > ARRAY_LINE_NUMBER))){
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }else{
- arrayForReturn = new int[size];
- for (int i = 0;i < size;i++){
- try {
- arrayForReturn[i] = Integer.parseInt(stringArray[i]);
- } catch (Exception e) {
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }
- }
- }
- }
- if(isCorrect) {
- return arrayForReturn;
- }else{
- return null;
- }
- }
- static int[] takeArray(final byte source){
- String inputPath;
- int[] inputArray;
- if (source == 1) {
- inputArray = takeArrayFromConsole().clone();
- } else {
- inputPath = takePathForInput();
- inputArray = takeArrayFromFile(inputPath);
- while (inputArray == null) {
- inputPath = takePathForInput();
- inputArray = takeArrayFromFile(inputPath);
- }
- }
- return inputArray;
- }
- static void swapArrayElements(int[] array, int i){
- array[i] ^= array[i + 1];
- array[i + 1] ^= array[i];
- array[i] ^= array[i + 1];
- }
- public static int[][] cutArray(int counter, int[][] answer){
- int[][] result = new int[counter][answer[0].length];
- for (int i = 0; i < counter; i++)
- result[i] = answer[i];
- return result;
- }
- public static int[][] findStagesOfCocktailSort(int[] array) {
- int[][] answer = new int[array.length][array.length];
- for (int k = 0; k < array.length; k++) {
- answer[0][k] = array[k];
- }
- int counter = 0;
- int leftBorder = 0, rightBorder = array.length - 1;
- boolean wasThereSwap = true;
- while ((leftBorder < rightBorder) && wasThereSwap) {
- wasThereSwap = false;
- for (int i = leftBorder; i < rightBorder; i++) {
- if (array[i] > array[i + 1]) {
- swapArrayElements(array, i);
- wasThereSwap = true;
- }
- }
- if(wasThereSwap){
- for (int k = 0; k < array.length; k++){
- answer[counter + 1][k] = array[k];
- }
- }
- counter++;
- wasThereSwap = false;
- rightBorder--;
- for (int i = rightBorder; i > leftBorder; i--) {
- if (array[i - 1] > array[i]) {
- swapArrayElements(array, i - 1);
- wasThereSwap = true;
- }
- }
- leftBorder++;
- if(wasThereSwap){
- for (int k = 0; k < array.length; k++){
- answer[counter + 1][k] = array[k];
- }
- }
- counter++;
- }
- return cutArray(counter, answer);
- }
- static void outputToConsole(int[][] answer){
- System.out.println("CocktailSort:");
- for (int i = 0; i < answer.length; i++){
- for (int j = 0; j < answer[0].length; j++)
- System.out.print(answer[i][j] + " ");
- System.out.println();
- }
- }
- static void outputToFile(final String path,final int[][] answer){
- boolean isCorrect = true;
- try(FileWriter fw = new FileWriter(path)) {
- fw.write("CocktailSort:\n");
- for (int i = 0; i < answer.length; i++){
- for (int j = 0; j < answer[0].length; j++)
- fw.write((answer[i][j]) + " ");
- fw.write("\n");
- }
- } catch (Exception e) {
- System.out.println("File error!");
- isCorrect = false;
- }
- if (isCorrect) {
- System.out.println("Done");
- } else {
- System.out.println("End");
- }
- }
- static void output(final byte source,final int[][] answer){
- String outPath;
- if (source == 1) {
- outputToConsole(answer);
- } else{
- outPath = takePathForOutput();
- outputToFile(outPath, answer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement