what is wrong with the following method that aims to fill an array with random numbers
Trouble: Given a sorted array arr[] of northward elements, write a office to search a given element x in arr[].
Examples:
Input: arr[] = {10, 20, 30, 50, 60, fourscore, 110, 130, 140, 170}, x = 110
Output: half-dozen
Caption: Element x is present at alphabetize 6Input: arr[] = {10, twenty, 30, forty, threescore, 110, 120, 130, 170}, ten = 175
Output: -1
Explanation: Chemical element x is non nowadays in arr[].
Linear Search Approach: A simple arroyo is to do a linear search . The time complication of the Linear search is O(northward). Another approach to perform the same chore is using Binary Search.
Binary Search Approach:
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to utilise the information that the assortment is sorted and reduce the time complexity to O(Log northward).
Binary Search Algorithm: The basic steps to perform Binary Search are:
- Brainstorm with an interval covering the whole assortment.
- If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower one-half.
- Otherwise, narrow it to the upper half.
- Repeatedly bank check until the value is establish or the interval is empty.
Illustration of Binary Search Algorithm:
Step-by-step Binary Search Algorithm: We basically ignore half of the elements just later on one comparing.
- Compare 10 with the middle chemical element.
- If x matches with the middle chemical element, we return the mid index.
- Else If x is greater than the mid element, and then x can only lie in the right half subarray after the mid element. And then we recur for the right half.
- Else (x is smaller) recur for the left half.
Recursive implementation of Binary Search :
C++
#include <$.25/stdc++.h>
using
namespace
std;
int
binarySearch(
int
arr[],
int
l,
int
r,
int
x)
{
if
(r >= l) {
int
mid = l + (r - l) / 2;
if
(arr[mid] == x)
return
mid;
if
(arr[mid] > x)
return
binarySearch(arr, l, mid - ane, x);
render
binarySearch(arr, mid + 1, r, ten);
}
return
-1;
}
int
chief(
void
)
{
int
arr[] = { two, three, four, ten, 40 };
int
10 = ten;
int
northward =
sizeof
(arr) /
sizeof
(arr[0]);
int
result = binarySearch(arr, 0, due north - 1, x);
(result == -one)
? cout <<
"Element is not present in array"
: cout <<
"Element is present at index "
<< effect;
render
0;
}
C
#include <stdio.h>
int
binarySearch(
int
arr[],
int
l,
int
r,
int
x)
{
if
(r >= l) {
int
mid = l + (r - l) / 2;
if
(arr[mid] == x)
return
mid;
if
(arr[mid] > x)
render
binarySearch(arr, l, mid - 1, x);
return
binarySearch(arr, mid + 1, r, x);
}
return
-one;
}
int
main(
void
)
{
int
arr[] = { 2, three, 4, 10, 40 };
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
int
10 = 10;
int
result = binarySearch(arr, 0, n - one, 10);
(result == -i)
?
printf
(
"Element is not present in array"
)
:
printf
(
"Element is present at index %d"
, issue);
render
0;
}
Java
class
BinarySearch {
int
binarySearch(
int
arr[],
int
fifty,
int
r,
int
x)
{
if
(r >= l) {
int
mid = l + (r - l) /
ii
;
if
(arr[mid] == ten)
return
mid;
if
(arr[mid] > 10)
return
binarySearch(arr, l, mid -
1
, 10);
return
binarySearch(arr, mid +
1
, r, ten);
}
return
-
1
;
}
public
static
void
main(String args[])
{
BinarySearch ob =
new
BinarySearch();
int
arr[] = {
2
,
iii
,
iv
,
x
,
xl
};
int
north = arr.length;
int
ten =
10
;
int
issue = ob.binarySearch(arr,
0
, due north -
1
, x);
if
(result == -
1
)
System.out.println(
"Element not present"
);
else
Organization.out.println(
"Chemical element found at index "
+ result);
}
}
Python3
def
binarySearch(arr, l, r, x):
if
r >
=
l:
mid
=
l
+
(r
-
50)
/
/
2
if
arr[mid]
=
=
x:
return
mid
elif
arr[mid] > 10:
return
binarySearch(arr, l, mid
-
one
, x)
else
:
render
binarySearch(arr, mid
+
i
, r, x)
else
:
return
-
1
arr
=
[
2
,
3
,
iv
,
10
,
twoscore
]
x
=
10
result
=
binarySearch(arr,
0
,
len
(arr)
-
1
, x)
if
issue !
=
-
one
:
print
(
"Chemical element is present at index % d"
%
consequence)
else
:
print
(
"Element is not present in assortment"
)
C#
using
Organization;
course
GFG {
static
int
binarySearch(
int
[] arr,
int
l,
int
r,
int
x)
{
if
(r >= l) {
int
mid = l + (r - l) / 2;
if
(arr[mid] == x)
return
mid;
if
(arr[mid] > x)
return
binarySearch(arr, l, mid - 1, x);
render
binarySearch(arr, mid + 1, r, x);
}
return
-one;
}
public
static
void
Master()
{
int
[] arr = { two, three, iv, 10, 40 };
int
n = arr.Length;
int
10 = 10;
int
result = binarySearch(arr, 0, n - i, ten);
if
(outcome == -one)
Console.WriteLine(
"Chemical element not present"
);
else
Console.WriteLine(
"Element found at index "
+ effect);
}
}
PHP
<?php
function
binarySearch(
$arr
,
$l
,
$r
,
$10
)
{
if
(
$r
>=
$l
)
{
$mid
=
ceil
(
$l
+ (
$r
-
$l
) / ii);
if
(
$arr
[
$mid
] ==
$x
)
return
flooring
(
$mid
);
if
(
$arr
[
$mid
] >
$x
)
render
binarySearch(
$arr
,
$50
,
$mid
- ane,
$x
);
render
binarySearch(
$arr
,
$mid
+ 1,
$r
,
$10
);
}
render
-1;
}
$arr
=
assortment
(two, 3, 4, ten, forty);
$n
=
count
(
$arr
);
$x
= 10;
$result
= binarySearch(
$arr
, 0,
$due north
- 1,
$x
);
if
((
$result
== -1))
echo
"Element is not present in array"
;
else
repeat
"Element is present at alphabetize "
,
$result
;
?>
Javascript
<script>
function
binarySearch(arr, l, r, x){
if
(r >= l) {
allow mid = l + Math.floor((r - l) / 2);
if
(arr[mid] == 10)
return
mid;
if
(arr[mid] > x)
render
binarySearch(arr, l, mid - i, x);
return
binarySearch(arr, mid + 1, r, ten);
}
render
-1;
}
let arr = [ 2, three, 4, 10, xl ];
permit x = 10;
let n = arr.length
let result = binarySearch(arr, 0, n - i, ten);
(result == -1) ? document.write(
"Element is non present in assortment"
)
: document.write(
"Element is nowadays at index "
+result);
</script>
Output
Element is present at index 3
Iterative implementation of Binary Search
C++
#include <bits/stdc++.h>
using
namespace
std;
int
binarySearch(
int
arr[],
int
50,
int
r,
int
x)
{
while
(l <= r) {
int
grand = l + (r - fifty) / 2;
if
(arr[yard] == x)
return
m;
if
(arr[yard] < 10)
l = m + i;
else
r = m - ane;
}
return
-1;
}
int
principal(
void
)
{
int
arr[] = { 2, 3, 4, 10, forty };
int
10 = x;
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
int
consequence = binarySearch(arr, 0, n - ane, 10);
(effect == -1)
? cout <<
"Chemical element is not nowadays in array"
: cout <<
"Element is present at index "
<< result;
return
0;
}
C
#include <stdio.h>
int
binarySearch(
int
arr[],
int
l,
int
r,
int
x)
{
while
(50 <= r) {
int
m = l + (r - l) / 2;
if
(arr[m] == x)
render
g;
if
(arr[chiliad] < x)
l = m + 1;
else
r = m - ane;
}
return
-1;
}
int
main(
void
)
{
int
arr[] = { ii, 3, 4, 10, twoscore };
int
north =
sizeof
(arr) /
sizeof
(arr[0]);
int
x = ten;
int
result = binarySearch(arr, 0, due north - 1, 10);
(result == -1) ?
printf
(
"Element is non present"
" in array"
)
:
printf
(
"Element is present at "
"index %d"
,
result);
render
0;
}
Java
class
BinarySearch {
int
binarySearch(
int
arr[],
int
ten)
{
int
l =
0
, r = arr.length -
ane
;
while
(l <= r) {
int
m = 50 + (r - l) /
two
;
if
(arr[m] == x)
render
thousand;
if
(arr[chiliad] < ten)
fifty = one thousand +
1
;
else
r = m -
1
;
}
render
-
1
;
}
public
static
void
master(String args[])
{
BinarySearch ob =
new
BinarySearch();
int
arr[] = {
2
,
iii
,
4
,
ten
,
40
};
int
n = arr.length;
int
x =
10
;
int
result = ob.binarySearch(arr, x);
if
(outcome == -
ane
)
System.out.println(
"Element not present"
);
else
System.out.println(
"Element found at "
+
"index "
+ issue);
}
}
Python3
def
binarySearch(arr, l, r, x):
while
l <
=
r:
mid
=
l
+
(r
-
l)
/
/
2
if
arr[mid]
=
=
x:
return
mid
elif
arr[mid] < x:
l
=
mid
+
ane
else
:
r
=
mid
-
1
return
-
1
arr
=
[
2
,
3
,
4
,
10
,
40
]
x
=
ten
result
=
binarySearch(arr,
0
,
len
(arr)
-
1
, x)
if
result !
=
-
1
:
print
(
"Element is present at alphabetize % d"
%
result)
else
:
print
(
"Element is not present in array"
)
C#
using
System;
grade
GFG {
static
int
binarySearch(
int
[] arr,
int
10)
{
int
l = 0, r = arr.Length - 1;
while
(l <= r) {
int
thou = l + (r - l) / 2;
if
(arr[m] == 10)
return
1000;
if
(arr[m] < 10)
50 = yard + 1;
else
r = m - 1;
}
return
-ane;
}
public
static
void
Primary()
{
int
[] arr = { ii, iii, 4, 10, 40 };
int
northward = arr.Length;
int
x = 10;
int
outcome = binarySearch(arr, x);
if
(consequence == -1)
Panel.WriteLine(
"Element not nowadays"
);
else
Console.WriteLine(
"Element institute at "
+
"index "
+ result);
}
}
PHP
<?php
office
binarySearch(
$arr
,
$fifty
,
$r
,
$ten
)
{
while
(
$l
<=
$r
)
{
$m
=
$fifty
+ (
$r
-
$50
) / two;
if
(
$arr
[
$m
] ==
$10
)
return
floor
(
$grand
);
if
(
$arr
[
$m
] <
$10
)
$l
=
$thousand
+ i;
else
$r
=
$m
- 1;
}
return
-one;
}
$arr
=
array
(ii, 3, 4, x, xl);
$n
=
count
(
$arr
);
$ten
= x;
$result
= binarySearch(
$arr
, 0,
$n
- 1,
$x
);
if
((
$upshot
== -1))
echo
"Element is non nowadays in array"
;
else
echo
"Element is present at index "
,
$upshot
;
?>
Javascript
<script>
part
binarySearch(arr, x)
{
let l = 0;
let r = arr.length - 1;
let mid;
while
(r >= fifty) {
mid = l + Math.floor((r - fifty) / two);
if
(arr[mid] == 10)
return
mid;
if
(arr[mid] > x)
r = mid - one;
else
fifty = mid + i;
}
return
-ane;
}
arr =
new
Array(2, 3, four, 10, 40);
x = x;
n = arr.length;
result = binarySearch(arr, x);
(effect == -ane) ? document.write(
"Element is not present in array"
)
: certificate.write (
"Element is present at index "
+ result);
</script>
Output
Chemical element is present at index 3
Algorithmic Paradigm: Decrease and Conquer.
Note: Hither we are using
int mid = low + (high – depression)/ii;
Possibly, you wonder why we are computing the center index this way, we tin can simply add the lower and higher index and divide it by two.
int mid = (low + high)/2;
Just if we calculate the middle index like this means our code is not 100% correct, it contains bugs.
That is, it fails for larger values of int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value(two31 – one ).
The sum overflows to a negative value and the value stays negative when divided past 2.
In java, it throws ArrayIndexOutOfBoundException.
int mid = depression + (loftier – low)/ii;
So information technology'south better to use information technology like this. This bug applies equally to merge sort and other divide and conquer algorithms.
GeeksforGeeks Courses:
i. Linguistic communication Foundation Courses [ C++ / Java / Python ]
Learn any programming language from scratch and understand all its fundamentals concepts for a strong programming foundation in the easiest possible way with help of GeeksforGeeks Linguistic communication Foundation Courses – Java Foundation | Python Foundation | C++ Foundation
2. Geeks Classes Live
Get interview-centric live online classes on Information Structure and Algorithms from any geographical location to learn and primary DSA concepts for enhancing your problem-solving & programming skills and to cleft the interview of whatever product-based company – Geeks Classes: Live Session
3. Complete Interview Preparation
Go fulfilled all your interview grooming needs at a unmarried identify with the Complete Interview Preparation Course that provides you all the required stuff to prepare for any product-based, service-based, or showtime-upward visitor at the most affordable prices.
four. DSA Self Paced
Commencement learning Data Structures and Algorithms to set for the interviews of summit IT giants like Microsoft, Amazon, Adobe, etc. with DSA Cocky-Paced Course where you will get to learn and master DSA from basic to avant-garde level and that besides at your own pace and convenience.
5. Company Specific Courses – Amazon , Microsoft , TCS & Wipro
Crack the interview of any product-based giant company by specifically preparing with the questions that these companies ordinarily ask in their coding interview round. Refer GeeksforGeeks Visitor Specific Courses: Amazon SDE Test Series, etc.
Source: https://www.geeksforgeeks.org/binary-search/
0 Response to "what is wrong with the following method that aims to fill an array with random numbers"
Post a Comment