-
Notifications
You must be signed in to change notification settings - Fork 1k
/
JumpSearch.cs
94 lines (78 loc) · 2.08 KB
/
JumpSearch.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
C# program for jump Search
Jump Search -
Jump Search is a searching algorithm for sorted arrays. The basic idea
is to check fewer elements (than linear search) by jumping ahead by
fixed steps or skipping some elements in place of searching all elements.
*/
using System;
public class JumpSearch
{
public static int JumpSearchAlgo(int[] arr, int x)
{
int n = arr.Length;
//Finding Block size to be jumped
int step = (int)Math.Floor(Math.Sqrt(n));
//Finding the block where element is present (if it is present)
int prev = 0;
while (arr[Math.Min(step, n) - 1] < x)
{
prev = step;
step += (int)Math.Floor(Math.Sqrt(n));
if (prev >= n)
return -1;
}
//Doing a linear search for x in block beginning with prev.
while (arr[prev] < x)
{
prev++;
//if we reached next block or end of array,element is not present.
if (prev == Math.Min(step, n))
return -1;
}
//if element is found
if (arr[prev] == x)
return prev;
return -1;
}
public static void Main()
{
Console.WriteLine("Enter size of array that you like to create");
int n = int.Parse( Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("Enter values in array");
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse( Console.ReadLine());
}
Console.WriteLine("Enter value that you like to find in array");
int x = int.Parse(Console.ReadLine());
int index = JumpSearchAlgo(arr, x);
if (index >= 0)
Console.WriteLine("Found at index: " + index);
else
Console.WriteLine(x + " isn't present in the array");
}
}
/*
Sample Input
Enter size of array that you like to create
10
Enter values in array
12
23
34
45
56
67
78
89
90
100
Enter value that you like to find in array
45
Sample Output
Found at index: 3
Time Complexity - O(√n)
Space Complexity - O(1)
*/