@@ -26,7 +26,7 @@ public static void main(String[] args) {
26
26
System .out .println ("\n **************** Performing Recursive Binary seaching ********************" );
27
27
System .out .print ("\n Enter the element to be searched : " );
28
28
double target2Item = scInp .nextDouble ();
29
- int result2Index =recursiveBinarySearching (inputArr ,targetItem ,0 ,inputArr .length -1 );
29
+ int result2Index =recursiveBinarySearching (inputArr ,target2Item ,0 ,inputArr .length -1 );
30
30
System .out .println ("\n Target element found at index : " +result2Index );
31
31
}
32
32
@@ -38,23 +38,22 @@ public static void main(String[] args) {
38
38
* @return resultIndex - int
39
39
*
40
40
*/
41
- private static int performBinarySearchingOnSortedArray (double [] inputArr ,double targetItem ) {
42
- int resultIndex = -1 ;
43
- int startIndex = 0 ;
44
- int endIndex = inputArr .length - 1 ;
45
- int midIndex ;
46
- while (startIndex <= endIndex ) {
47
- midIndex =( startIndex + endIndex / 2 );
48
- if (inputArr [midIndex ]== targetItem ) {
49
- return midIndex ;
50
- }
51
- if (inputArr [midIndex ]< targetItem ) {
52
- return startIndex = midIndex + 1 ;
53
- }
54
- else {
55
- return endIndex = midIndex - 1 ;
41
+ private static int performBinarySearchingOnSortedArray (double [] inputArr ,double targetItem ) {
42
+ int resultIndex = -1 ;
43
+ int startIndex = 0 ;
44
+ int endIndex = inputArr .length - 1 ;
45
+ int midIndex ;
46
+ while (startIndex <= endIndex ) {
47
+ midIndex = (( startIndex + endIndex ) / 2 );
48
+ if (inputArr [midIndex ] == targetItem ) {
49
+ return midIndex ;
50
+ }
51
+ if (inputArr [midIndex ] < targetItem ) {
52
+ startIndex = midIndex + 1 ;
53
+ } else {
54
+ endIndex = midIndex - 1 ;
55
+ }
56
56
}
57
- }
58
57
return resultIndex ;
59
58
}
60
59
@@ -66,25 +65,22 @@ private static int performBinarySearchingOnSortedArray(double[] inputArr, double
66
65
* @return resultIndex - int
67
66
*
68
67
*/
69
- private static int recursiveBinarySearching (double [] inputArr , double targetItem ,int startIndex ,int endIndex ) {
70
- int resultIndex =-1 ;
71
- int midIndex ;
72
- if (endIndex <startIndex )
73
- {
74
- return resultIndex ;
75
- }
76
- midIndex =(startIndex +endIndex /2 );
77
- if (inputArr [midIndex ]==targetItem ) {
78
- return midIndex ;
79
- }
80
- if (inputArr [midIndex ]<targetItem ) {
81
- startIndex =midIndex +1 ;
82
- return recursiveBinarySearching (inputArr ,targetItem ,startIndex ,endIndex );
83
- }
84
- else {
85
- endIndex =midIndex -1 ;
86
- return recursiveBinarySearching (inputArr ,targetItem ,startIndex ,endIndex );
87
- }
68
+ private static int recursiveBinarySearching (double [] inputArr , double targetItem , int startIndex , int endIndex ) {
69
+ int resultIndex = -1 ;
70
+ if (endIndex < startIndex ) {
71
+ return resultIndex ;
72
+ }
73
+ int midIndex = ((startIndex + endIndex ) / 2 );
74
+ if (inputArr [midIndex ] == targetItem ) {
75
+ return midIndex ;
76
+ }
77
+ if (inputArr [midIndex ] < targetItem ) {
78
+ startIndex = midIndex + 1 ;
79
+ return recursiveBinarySearching (inputArr , targetItem , startIndex , endIndex );
80
+ } else {
81
+ endIndex = midIndex - 1 ;
82
+ return recursiveBinarySearching (inputArr , targetItem , startIndex , endIndex );
83
+ }
88
84
}
89
85
90
86
0 commit comments