Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ъimport java.util.Scanner;
- import java.io.*;
- public class BinarySearch {
- public static Scanner scan = new Scanner(System.in);
- static Scanner fileScanner;
- public static void main(String[] args) {
- int num;
- int key;
- int[] arr;
- System.out.println("Program implements Binary Search - search of element in array");
- System.out.println("Enter type of input." + '\n' + "1 is console input, 0 is file input.");
- String path;
- boolean chose = choose();
- boolean isIncorrect;
- if (chose) {
- System.out.println("Input size of array:");
- num = inputArraySize();
- do {
- System.out.println("Input array elements:");
- arr = inputArray(num);
- isIncorrect = checkArray(arr);
- } while (!isIncorrect);
- System.out.println("Input number for search:");
- key = inputData();
- } else {
- do {
- path = inputFilePath();
- num = inputSizeOfArrayFromFile(path);
- arr = inputArrayFile(path, num);
- isIncorrect = checkArray(arr);
- } while (!isIncorrect);
- key = inputKeyFromFile(path);
- }
- output(arr, key, num);
- }
- private static void output (int [] arr, int key, int num){
- int result = searchElement(arr, key, 0, num - 1);
- if (result == -1) {
- System.out.println("The element you are looking for is not in the array.");
- } else {
- System.out.println("The item you are looking for is on " + (result + 1) + " place");
- }
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputNumber == 1;
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- private static int inputArraySize() {
- boolean isIncorrect;
- final int MIN_SIZE = 2;
- final int MAX_SIZE = 10;
- int num;
- do {
- num = inputData();
- isIncorrect = false;
- if ((num < MIN_SIZE) || (num > MAX_SIZE)) {
- System.out.println("Please, enter a number in the range from 2 to 10:");
- 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 boolean checkArray (int[] arr) {
- int i = 0;
- boolean ascending = true; // возрастание
- boolean descending = true; // убывание
- while ((i < arr.length - 1) && (descending)) {
- if (arr[i] <= arr[i + 1]) {
- descending = false; // проверка на убывание
- }
- i++;
- }
- i = 0;
- while ((i < arr.length - 1) && (ascending)) {
- if (arr[i] >= arr[i + 1]) {
- ascending = false; // проверка на возрастание
- }
- i++;
- }
- boolean isInCorrect = true;
- if ((!ascending) && (!descending)) {
- System.out.println("Enter a sorted array, since binary search is only possible in such.");
- isInCorrect = false;
- }
- return isInCorrect;
- }
- private static int searchElement(int[] arr, int key, int left, int right) {
- if (left <= right) {
- int mid = left + ((right - left) / 2);
- if (arr[mid] == key) {
- return mid;
- } else if (arr[mid] < key) {
- return searchElement(arr, key, mid + 1, right);
- } else {
- return searchElement(arr, key, left, mid - 1);
- }
- }
- return -1;
- }
- //files
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scan.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;
- final int MAX = 10;
- 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) || (num > MAX))) {
- isIncorrect = true;
- System.out.println("Matrix size should be in the range from 2 to 10:");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- private static int inputKeyFromFile(String path) {
- int num = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- fileScanner.nextLine();
- num = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file");
- path = inputFilePath();
- }
- } 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement