Trying to create a program using following algorithms.
JumpSearch(list,lower,upper,jump,x)
{Look for X in List[lower ... upper].
Report its position if found, else report 0. List is sorted in increasing order.
upper>=lower>0; upper-lower+1>=jump>=1.}
index = lower + jump -1
while upper > index and x> list [index]
index = index + jump
if upper > index
upper = index
LinearSearch(list,index-jump+1, upper, x)
This is so far I have, I don't the program is not correct. Can anyone help me complete it or guide it so that I can complete it.
{
class Program
{
public int JumpSearch (int[] list, int lower, int upper, int jump, int X)
{
int index = lower + jump - 1;
while (upper > index && X > list[index])
{
index = index + jump;
}
if (upper > index)
{ upper = index; }
return -1;
}
static void Main(string[] args)
{
int Index;
int Item;
int lower = 0, upper = 9;
int[] list = new int[10] {1,2,3,4,5,6,7,8,9,10};
Console.WriteLine("\nEnter the number you want to search the position.\n");
Item = int.Parse(Console.ReadLine());
Program p = new Program();
Index = p.JumpSearch(list, lower, upper, Item,1);
if (Index == -1)
{
Console.WriteLine("The number you entered could not be found. Please enter different number from the list.");
}
else
{
Console.WriteLine("The number you entered is found at {0} position in the list.", Index);
}
Console.ReadLine();
}
}
}
i like this programming
It looks like the program you have written is on the right track, but there are a few modifications and additions that need to be made. Here's an updated version of your program:
```csharp
using System;
class Program
{
public int JumpSearch(int[] list, int lower, int upper, int jump, int X)
{
int index = lower + jump - 1;
while (upper > index && X > list[index])
{
index = index + jump;
}
if (list[index] == X)
{
return index;
}
// Perform a linear search within the identified range if the element is not found
return LinearSearch(list, index - jump + 1, Math.Min(upper, index), X);
}
public int LinearSearch(int[] list, int lower, int upper, int X)
{
for (int i = lower; i <= upper; i++)
{
if (list[i] == X)
{
return i;
}
}
return -1;
}
static void Main(string[] args)
{
int Index;
int Item;
int lower = 0, upper = 9;
int[] list = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("Enter the number you want to search:");
Item = int.Parse(Console.ReadLine());
Program p = new Program();
Index = p.JumpSearch(list, lower, upper, 3, Item);
if (Index == -1)
{
Console.WriteLine("The number you entered could not be found.");
}
else
{
Console.WriteLine("The number you entered is found at position {0} in the list.", Index);
}
Console.ReadLine();
}
}
```
In this modified version:
1. I added a `LinearSearch` method which will be called when the element is not found within the jump range.
2. I changed the parameter `jump` in the `JumpSearch` method to 3. This is because the jump size should be a fixed value and not the number we're searching for.
3. I added a check within the `JumpSearch` method to return the index if the element is found.
4. I used the `Math.Min` function to determine the upper limit when performing the linear search.
Now, when you run the program, it should correctly perform a jump search followed by a linear search to find the desired element in the list.