Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Searching-algorithms/Binarysearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
20 changes: 17 additions & 3 deletions Searching-algorithms/Fibonaccisearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
19 changes: 18 additions & 1 deletion Searching-algorithms/Interpolationsearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
19 changes: 18 additions & 1 deletion Searching-algorithms/Uniform_binarysearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
Loading