diff --git a/Searching-algorithms/Binarysearch.java b/Searching-algorithms/Binarysearch.java index 327eee0..c86bbe5 100644 --- a/Searching-algorithms/Binarysearch.java +++ b/Searching-algorithms/Binarysearch.java @@ -6,12 +6,19 @@ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int size = sc.nextInt(); - System.out.println("Enter Array elements: "); + System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[size]; for (int i = 0; i < size; i++) { arr[i] = sc.nextInt(); } System.out.println("Entered Array: " + Arrays.toString(arr)); + + if (!isSorted(arr)) { + System.out.println("Invalid input: Binary Search requires a sorted array in non-decreasing order."); + sc.close(); + return; + } + System.out.println("Enter the key element to be Found: "); int key = sc.nextInt(); int result = binarysearch(arr, key); @@ -20,6 +27,7 @@ public static void main(String[] args) { } else { System.out.println("Element " + key + " was NOT Found in the entered Array."); } + sc.close(); } public static int binarysearch(int[] arr, int key) { int low = 0; @@ -35,4 +43,13 @@ else if (key < arr[mid]) } return -1; } + + public static boolean isSorted(int[] arr) { + for (int i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) { + return false; + } + } + return true; + } } diff --git a/Searching-algorithms/Fibonaccisearch.java b/Searching-algorithms/Fibonaccisearch.java index 2f4b82c..fc06bdb 100644 --- a/Searching-algorithms/Fibonaccisearch.java +++ b/Searching-algorithms/Fibonaccisearch.java @@ -9,13 +9,18 @@ public static void main(String[] args) { int size = sc.nextInt(); int[] arr = new int[size]; - System.out.println("Enter array elements: "); + System.out.println("Enter array elements in non-decreasing (sorted) order: "); for (int i = 0; i < size; i++) { arr[i] = sc.nextInt(); } - Arrays.sort(arr); - System.out.println("Sorted Array: " + Arrays.toString(arr)); + System.out.println("Entered Array: " + Arrays.toString(arr)); + + if (!isSorted(arr)) { + System.out.println("Invalid input: Fibonacci Search requires a sorted array in non-decreasing order."); + sc.close(); + return; + } System.out.println("Enter the key element to be Found: "); int key = sc.nextInt(); @@ -67,4 +72,13 @@ public static int fibonaccisearch(int[] arr, int key) { return -1; } + + public static boolean isSorted(int[] arr) { + for (int i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) { + return false; + } + } + return true; + } } diff --git a/Searching-algorithms/Interpolationsearch.java b/Searching-algorithms/Interpolationsearch.java index 41104e6..c4576a2 100644 --- a/Searching-algorithms/Interpolationsearch.java +++ b/Searching-algorithms/Interpolationsearch.java @@ -6,12 +6,19 @@ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int n = sc.nextInt(); - System.out.println("Enter Array elements: "); + System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println("Entered Array: " + Arrays.toString(arr)); + + if (!isSorted(arr)) { + System.out.println("Invalid input: Interpolation Search requires a sorted array in non-decreasing order."); + sc.close(); + return; + } + System.out.println("Enter the key element to be Found: "); int key = sc.nextInt(); int result = interpolationSearch(arr, 0, arr.length - 1, key); @@ -20,6 +27,7 @@ public static void main(String[] args) { } else { System.out.println("Element " + key + " was NOT Found in the entered Array."); } + sc.close(); } public static int interpolationSearch(int arr[], int lo, int hi, int x) { int pos; @@ -39,4 +47,13 @@ public static int interpolationSearch(int arr[], int lo, int hi, int x) { } return -1; } + + public static boolean isSorted(int[] arr) { + for (int i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) { + return false; + } + } + return true; + } } diff --git a/Searching-algorithms/Uniform_binarysearch.java b/Searching-algorithms/Uniform_binarysearch.java index 7b2fbb7..0f9ecab 100644 --- a/Searching-algorithms/Uniform_binarysearch.java +++ b/Searching-algorithms/Uniform_binarysearch.java @@ -6,12 +6,19 @@ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int size = sc.nextInt(); - System.out.println("Enter Array elements: "); + System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[size]; for (int i = 0; i < size; i++) { arr[i] = sc.nextInt(); } System.out.println("Entered Array: " + Arrays.toString(arr)); + + if (!isSorted(arr)) { + System.out.println("Invalid input: Uniform Binary Search requires a sorted array in non-decreasing order."); + sc.close(); + return; + } + System.out.println("Enter the key element to be Found: "); int key = sc.nextInt(); int result = binunisearch(arr, key); @@ -20,6 +27,7 @@ public static void main(String[] args) { } else { System.out.println("Element " + key + " was NOT Found in the entered Array."); } + sc.close(); } public static int binunisearch(int[] arr, int key) { int n = arr.length; @@ -42,4 +50,13 @@ public static int binunisearch(int[] arr, int key) { return index; return -1; } + + public static boolean isSorted(int[] arr) { + for (int i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) { + return false; + } + } + return true; + } }