-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchInNearlySortedArray.java
56 lines (49 loc) · 1.96 KB
/
SearchInNearlySortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.datastructure;
import java.util.Scanner;
public class SearchInNearlySortedArray {
static int SearchInNearlySortedArray(int array[], int target) {
int start = 0;
int n = array.length;
int end = n - 1;
//Iterative binary search
while (start <= end) {
int mid = (start + (end - start) / 2);
//count<<nums[mid]<<"\n";
if (array[mid] == target)
return mid;
else if (mid - 1 >= start && array[mid - 1] == target) //to avoid error if mid comes to start
return mid - 1;
else if (mid + 1 <= end && array[mid + 1] == target)
return mid + 1;
else if (array[mid] < target) {
start = mid + 2;
} else if (array[mid] > target) {
end = mid - 2;
} else {
System.out.println("Not found ");
}
}
return -1;
}
public static void main(String[] args) {
// int arr[] = {15, 18, 2, 3, 6, 12};
Scanner s = new Scanner(System.in); //new instance and calling the input func
System.out.println("Enter the length of the array:");
int length = s.nextInt(); //defining size and getting the input
int[] array = new int[length]; // defining array of length provided
System.out.println("Enter the elements of the array:");
for (int i = 0; i < length; i++) {
array[i] = s.nextInt();
}
// int target = s.nextInt();
int n = array.length;
System.out.println("Enter the number to search:");
int target = s.nextInt();
int index = SearchInNearlySortedArray(array, target);
if (index != -1) {
System.out.println("Element found at index " + index);
} else {
System.out.println("Element not found in the array");
}
}
}