Nguyễn Đức Thành
New member
Which of the following code correctly represent jump search?
A.
B.
C.
D.
A.
Code:
int jumpSearch(int arr[], int x, int n)
{
int step = n*n;
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
while (arr[prev] < x)
{
prev++;
if (prev == min(step, n))
return -1;
}
if (arr[prev] == x)
return prev;
return -1;
}
Code:
int jumpSearch(int arr[], int x, int n)
{
int step = sqrt(n);
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
while (arr[prev] < x)
{
prev++;
if (prev == min(step, n))
return -1;
}
if (arr[prev] == x)
return prev;
return -1;
}
Code:
int jumpSearch(int arr[], int x, int n)
{
int step = sqrt(n);
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev == n)
return -1;
}
while (arr[prev] < x)
{
prev++;
if (prev == min(step, n))
return -1;
}
if (arr[prev] == x)
return prev;
return -1;
}
Code:
int jumpSearch(int arr[], int x, int n)
{
int step = sqrt(n);
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
while (arr[prev] > x)
{
prev++;
if (prev == min(step, n))
return -1;
}
if (arr[prev] == x)
return prev;
return -1;
}