Kotlin Program to Implement Jump Search

Kotlin Program to Implement Jump Search

Searching efficiently is an essential skill in programming. When dealing with large, sorted datasets, using a simple Linear Search can be slow because it checks each element one by one. Jump Search is an algorithm that improves search speed by “jumping” ahead by fixed steps instead of checking every element, and then performing a linear search within a smaller block if needed. This makes it faster than Linear Search while being simpler than Binary Search in some cases.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Jump Search is especially useful when the dataset is sorted and random access is possible, such as arrays or lists in Kotlin. It is often applied in searching ordered data in applications like student records, product inventories, or any list where accessing an element by index is easy. For beginners, implementing Jump Search in Kotlin offers a hands-on opportunity to understand step-wise search strategies and how to optimize algorithm performance.

Program 1: Basic Jump Search on Array

This program demonstrates the standard Jump Search using a block size based on the square root of the array length.

import kotlin.math.sqrt

fun jumpSearch(arr: IntArray, target: Int): Int {

    val n = arr.size

    if (n == 0) return -1

    val step = sqrt(n.toDouble()).toInt()
    var prev = 0
    var current = step

    // Jump ahead until target might be within [prev, current)
    while (prev < n && arr[minOf(current, n) - 1] < target) {

        prev = current
        current += step

        if (prev >= n) return -1

    }

    // Linear search in the found block
    for (i in prev until minOf(current, n)) {
        if (arr[i] == target) return i
    }

    return -1

}

fun main() {

    val numbers = intArrayOf(1, 3, 5, 7, 9, 11, 13)
    val target = 9

    println("Array: ${numbers.joinToString()}")

    val index = jumpSearch(numbers, target)

    if (index != -1)
        println("Element $target found at index $index")
    else
        println("Element $target not found")

}

In this example, the algorithm jumps ahead by the calculated step size and then performs a linear search in the block where the target may exist. Beginners can see how combining jumps and linear search reduces the number of comparisons.

Program 2: Jump Search Using While Loop

Sometimes a while loop gives better control when iterating through the array. This version shows how to use a while loop for Jump Search.

import kotlin.math.sqrt

fun jumpSearchWhile(arr: IntArray, target: Int): Int {

    val n = arr.size
    val step = sqrt(n.toDouble()).toInt()
    var prev = 0
    var current = step

    while (current <= n && arr[Math.min(current, n) - 1] < target) {
        prev = current
        current += step
    }

    var i = prev

    while (i < Math.min(current, n)) {

        if (arr[i] == target) return i
        i++

    }

    return -1

}

fun main() {

    val numbers = intArrayOf(2, 4, 6, 8, 10, 12)
    val target = 8

    println("Array: ${numbers.joinToString()}")

    val index = jumpSearchWhile(numbers, target)

    if (index != -1) println("Element $target found at index $index")
    else println("Element $target not found")

}

Using a while loop allows beginners to understand how loops can control both the jump and the final linear search efficiently.

Program 3: Jump Search on Kotlin List

Jump Search can also be applied to Kotlin lists, which are often used in real-world applications. This program demonstrates how to implement it on a List<Int>.

import kotlin.math.sqrt

fun jumpSearchList(list: List<Int>, target: Int): Int {

    val n = list.size
    val step = sqrt(n.toDouble()).toInt()
    var prev = 0
    var current = 0

    while (current < n && list[current] < target) {

        prev = current
        current += step

        if (current > n) current = n

    }

    for (i in prev until current) {
        if (list[i] == target) return i
    }

    return -1

}

fun main() {

    val numbers = listOf(3, 6, 9, 12, 15, 18)
    val target = 12

    println("List: $numbers")

    val index = jumpSearchList(numbers, target)

    if (index != -1) println("Element $target found at index $index")
    else println("Element $target not found")

}

This shows that Jump Search is versatile and can be applied to different Kotlin collection types, making it useful for real projects.

Program 4: Jump Search with Duplicates

Sometimes arrays contain duplicate elements. This version demonstrates finding the first occurrence of a target value using Jump Search.

import kotlin.math.sqrt

fun jumpSearchFirst(arr: IntArray, target: Int): Int {

    val n = arr.size
    val step = sqrt(n.toDouble()).toInt()
    var prev = 0
    var current = 0
    var result = -1

    while (current < n && arr[current] < target) {

        prev = current
        current += step

        if (current > n) current = n

    }

    for (i in prev until current) {

        if (arr[i] == target) {
            result = i
            break
        }

    }

    return result

}

fun main() {

    val numbers = intArrayOf(1, 3, 3, 5, 7)
    val target = 3

    println("Array: ${numbers.joinToString()}")

    val index = jumpSearchFirst(numbers, target)

    if (index != -1) println("First occurrence of $target is at index $index")
    else println("$target not found")

}

By finding the first occurrence, this program demonstrates how Jump Search can be adapted for specific requirements, which is a useful skill for beginners learning algorithm design.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about Jump Search in Kotlin.

Q1: What is Jump Search?
Jump Search is a search algorithm that jumps ahead by fixed steps in a sorted dataset and then performs a linear search in the block where the target may exist.

Q2: When should I use Jump Search?
It is ideal for large, sorted datasets where random access is possible, offering faster performance than linear search.

Q3: Can Jump Search work with lists and arrays?
Yes, it works for both arrays and Kotlin lists, making it versatile for different applications.

Q4: What is the time complexity?
The time complexity is O(√n), which is faster than linear search for large datasets.

Q5: How is the step size determined?
The step size is usually the square root of the dataset size, balancing the number of jumps and linear checks efficiently.

Conclusion

Jump Search is a practical algorithm for searching sorted datasets efficiently. It combines the advantages of block-wise jumps with a linear search in smaller segments, reducing the number of comparisons compared to simple linear search. In Kotlin, Jump Search can be implemented on arrays or lists, iteratively or with modifications for duplicates, providing a versatile tool for real-world applications. Beginners can practice this algorithm to understand step-wise search strategies and optimize search performance in their programs.

Scroll to Top