|
| 1 | +--- |
| 2 | +id: Jump-Search |
| 3 | +title: Jump Search (Geeks for Geeks) |
| 4 | +sidebar_label: Jump Search |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Search Algorithms |
| 8 | + - Geeks for Geeks |
| 9 | + - CPP |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - JavaScript |
| 13 | + - DSA |
| 14 | +description: "This is a solution to the Jump Search problem." |
| 15 | +--- |
| 16 | + |
| 17 | +## What is Jump Search? |
| 18 | + |
| 19 | +Jump Search is an efficient search algorithm for sorted arrays. It works by jumping ahead by fixed steps and then performing a linear search within a block, making it faster than linear search but less complex than binary search. |
| 20 | + |
| 21 | +## Algorithm for Jump Search |
| 22 | + |
| 23 | +1. Calculate the optimal step size $\sqrt{N}$, where $N$ is the length of the list. |
| 24 | +2. Start from the first element and jump ahead by the step size until the target element is greater than or equal to the current element. |
| 25 | +3. Perform a linear search within the identified block. |
| 26 | +4. If the target element is found, return its index. |
| 27 | +5. If the target element is not found, return -1. |
| 28 | + |
| 29 | +## How does Jump Search work? |
| 30 | + |
| 31 | +- It calculates a jump step based on the length of the list. |
| 32 | +- It jumps ahead in blocks, comparing the target value with the current element at each step. |
| 33 | +- Once the block where the target might be located is identified, a linear search within the block is performed. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Problem Description |
| 38 | + |
| 39 | +Given a sorted list and a target element, implement the Jump Search algorithm to find the index of the target element in the list. If the element is not present, return -1. |
| 40 | + |
| 41 | +## Examples |
| 42 | + |
| 43 | +**Example 1:** |
| 44 | +Input: |
| 45 | +list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 46 | +target = 6 |
| 47 | +Output: 6 |
| 48 | + |
| 49 | + |
| 50 | +**Example 2:** |
| 51 | +Input: |
| 52 | +list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 53 | +target = 15 |
| 54 | +Output: -1 |
| 55 | + |
| 56 | + |
| 57 | +## Your Task: |
| 58 | + |
| 59 | +You don't need to read input or print anything. Complete the function jump_search() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1. |
| 60 | + |
| 61 | +Expected Time Complexity: $O(\sqrt{N})$ |
| 62 | +Expected Auxiliary Space: $O(1)$ |
| 63 | + |
| 64 | +## Constraints |
| 65 | + |
| 66 | +- $1 <= N <= 10^5$ |
| 67 | +- $1 <= arr[i] <= 10^6$ |
| 68 | +- $1 <= K <= 10^6$ |
| 69 | + |
| 70 | +## Implementation |
| 71 | + |
| 72 | +<Tabs> |
| 73 | + <TabItem value="Python" label="Python" default> |
| 74 | + |
| 75 | +```python |
| 76 | + import math |
| 77 | + |
| 78 | + def jump_search(lst, target): |
| 79 | + length = len(lst) |
| 80 | + step = int(math.sqrt(length)) |
| 81 | + prev = 0 |
| 82 | + |
| 83 | + while lst[min(step, length) - 1] < target: |
| 84 | + prev = step |
| 85 | + step += int(math.sqrt(length)) |
| 86 | + if prev >= length: |
| 87 | + return -1 |
| 88 | + |
| 89 | + for i in range(prev, min(step, length)): |
| 90 | + if lst[i] == target: |
| 91 | + return i |
| 92 | + return -1 |
| 93 | + ``` |
| 94 | + </TabItem> |
| 95 | + <TabItem value="C++" label="C++"> |
| 96 | + |
| 97 | + ```cpp |
| 98 | + #include <iostream> |
| 99 | + #include <cmath> |
| 100 | + #include <vector> |
| 101 | +int jump_search(const std::vector<int>& lst, int target) { |
| 102 | +int length = lst.size(); |
| 103 | +int step = sqrt(length); |
| 104 | +int prev = 0; |
| 105 | + while (lst[std::min(step, length) - 1] < target) { |
| 106 | + prev = step; |
| 107 | + step += sqrt(length); |
| 108 | + if (prev >= length) { |
| 109 | + return -1; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for (int i = prev; i < std::min(step, length); ++i) { |
| 114 | + if (lst[i] == target) { |
| 115 | + return i; |
| 116 | + } |
| 117 | + } |
| 118 | + return -1; |
| 119 | +} |
| 120 | + |
| 121 | +int main() { |
| 122 | +std::vector<int> lst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 123 | +int target = 6; |
| 124 | +std::cout << "Index: " << jump_search(lst, target) << std::endl; |
| 125 | +return 0; |
| 126 | +} |
| 127 | + |
| 128 | +``` |
| 129 | +</TabItem> |
| 130 | +
|
| 131 | +<TabItem value="Java" label="Java"> |
| 132 | + |
| 133 | +```java |
| 134 | +import java.util.Arrays; |
| 135 | +
|
| 136 | +public class JumpSearch { |
| 137 | + public static int jumpSearch(int[] lst, int target) { |
| 138 | + int length = lst.length; |
| 139 | + int step = (int) Math.sqrt(length); |
| 140 | + int prev = 0; |
| 141 | +
|
| 142 | + while (lst[Math.min(step, length) - 1] < target) { |
| 143 | + prev = step; |
| 144 | + step += (int) Math.sqrt(length); |
| 145 | + if (prev >= length) { |
| 146 | + return -1; |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + for (int i = prev; i < Math.min(step, length); i++) { |
| 151 | + if (lst[i] == target) { |
| 152 | + return i; |
| 153 | + } |
| 154 | + } |
| 155 | + return -1; |
| 156 | + } |
| 157 | +
|
| 158 | + public static void main(String[] args) { |
| 159 | + int[] lst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 160 | + int target = 6; |
| 161 | + System.out.println("Index: " + jumpSearch(lst, target)); |
| 162 | + } |
| 163 | +} |
| 164 | +
|
| 165 | +``` |
| 166 | + |
| 167 | +</TabItem> |
| 168 | + <TabItem value="JavaScript" label="JavaScript"> |
| 169 | + |
| 170 | + ```javascript |
| 171 | + function jumpSearch(lst, target) { |
| 172 | + let length = lst.length; |
| 173 | + let step = Math.floor(Math.sqrt(length)); |
| 174 | + let prev = 0; |
| 175 | + while (lst[Math.min(step, length) - 1] < target) { |
| 176 | + prev = step; |
| 177 | + step += Math.floor(Math.sqrt(length)); |
| 178 | + if (prev >= length) { |
| 179 | + return -1; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + for (let i = prev; i < Math.min(step, length); i++) { |
| 184 | + if (lst[i] === target) { |
| 185 | + return i; |
| 186 | + } |
| 187 | + } |
| 188 | + return -1; |
| 189 | +} |
| 190 | + |
| 191 | +const lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 192 | +const target = 6; |
| 193 | +console.log("Index:", jumpSearch(lst, target)); |
| 194 | +``` |
| 195 | + |
| 196 | +</TabItem> |
| 197 | +</Tabs> |
| 198 | + |
| 199 | +## Complexity Analysis |
| 200 | + |
| 201 | +- **Time Complexity**: $O(\sqrt{n})$, where $n$ is the number of elements in the list. The list is divided into blocks, leading to a root-time complexity. |
| 202 | +- **Space Complexity**: $O(1)$, as no extra space is required apart from the input list. |
| 203 | + |
| 204 | +## Advantages and Disadvantages |
| 205 | + |
| 206 | +**Advantages:** |
| 207 | +- Faster than linear search for large sorted lists. |
| 208 | +- Simpler than binary search while still being efficient. |
| 209 | + |
| 210 | +**Disadvantages:** |
| 211 | +- Requires the list to be sorted. |
| 212 | +- Less efficient compared to binary search in terms of time complexity. |
0 commit comments