Searching is one of the fundamental tasks in programming. Often, we need to locate a specific value in a dataset, whether it’s finding a student’s score, a product in inventory, or a number in an array. Linear Search is the simplest and most straightforward search algorithm that beginners can start with. It checks each element of the array or list sequentially until it finds the target value or reaches the end.
with hands-on learning.
get the skills and confidence to land your next move.
Linear Search is easy to understand and implement, making it an excellent starting point for learning about search algorithms. While it may not be the most efficient for large datasets, it works on both sorted and unsorted data, and it forms the foundation for understanding more complex search techniques like binary search. In this article, we will explore multiple Kotlin implementations of Linear Search, covering loops, recursion, and practical variations for beginners.
Program 1: Linear Search Using a Simple Loop
This program demonstrates the classic linear search approach using a for loop to find an element in an array.
fun linearSearch(arr: IntArray, target: Int): Int {
for (i in arr.indices) {
if (arr[i] == target) return i
}
return -1
}
fun main() {
val numbers = intArrayOf(4, 2, 9, 1, 7, 5)
val target = 7
println("Array: ${numbers.joinToString()}")
val index = linearSearch(numbers, target)
if (index != -1) println("Element $target found at index $index")
else println("Element $target not found")
}In this example, the program checks each element of the array one by one. If the target value matches an element, its index is returned; otherwise, it returns -1. This is a simple and intuitive approach that beginners can easily understand.
Program 2: Linear Search Using a While Loop
Sometimes, a while loop can be more suitable for linear search. This approach achieves the same result but demonstrates an alternative loop structure.
fun linearSearchWhile(arr: IntArray, target: Int): Int {
var i = 0
while (i < arr.size) {
if (arr[i] == target) return i
i++
}
return -1
}
fun main() {
val numbers = intArrayOf(10, 15, 20, 25, 30)
val target = 25
println("Array: ${numbers.joinToString()}")
val index = linearSearchWhile(numbers, target)
if (index != -1) println("Element $target found at index $index")
else println("Element $target not found")
}Using a while loop gives beginners an understanding of how loops can be controlled with conditions, offering flexibility when traversing arrays or lists.
Program 3: Linear Search Using Recursion
Linear search can also be implemented recursively. This approach is elegant and shows how recursive calls can replace loops.
fun linearSearchRecursive(arr: IntArray, target: Int, index: Int = 0): Int {
if (index >= arr.size) return -1
if (arr[index] == target) return index
return linearSearchRecursive(arr, target, index + 1)
}
fun main() {
val numbers = intArrayOf(3, 6, 9, 12, 15)
val target = 12
println("Array: ${numbers.joinToString()}")
val index = linearSearchRecursive(numbers, target)
if (index != -1) println("Element $target found at index $index")
else println("Element $target not found")
}This recursive approach checks the current index for the target and calls itself for the next index until it finds the value or reaches the end. Beginners can learn how recursion can simplify the logic of sequential search.
Program 4: Linear Search on a Kotlin List
Kotlin lists are widely used, and linear search can be applied directly to mutable or immutable lists. This program demonstrates linear search on a MutableList.
fun linearSearchList(list: MutableList<Int>, target: Int): Int {
for (i in list.indices) {
if (list[i] == target) return i
}
return -1
}
fun main() {
val numbers = mutableListOf(8, 4, 2, 10, 6)
val target = 10
println("List: $numbers")
val index = linearSearchList(numbers, target)
if (index != -1) println("Element $target found at index $index")
else println("Element $target not found")
}This example demonstrates that linear search is not limited to arrays and can work seamlessly with Kotlin’s list structures, making it versatile for different scenarios.
Frequently Asked Questions (FAQ)
Here are some common questions beginners have about linear search in Kotlin.
Q1: What is Linear Search?
Linear Search is a simple algorithm that searches for a target element by checking each element sequentially.
Q2: When should I use Linear Search?
It is suitable for small datasets or unsorted data where other optimized search algorithms are not applicable.
Q3: Can Linear Search work with lists and arrays?
Yes, it works on both arrays and lists, and it can be easily adapted to different Kotlin collections.
Q4: What is the time complexity of Linear Search?
The worst-case time complexity is O(n), where n is the number of elements.
Q5: Can Linear Search be implemented recursively?
Yes, recursion can replace loops to check elements one by one, providing an elegant alternative approach.
Conclusion
Linear Search is a beginner-friendly and easy-to-understand algorithm for finding elements in arrays or lists. It works on sorted or unsorted data and can be implemented using loops or recursion. In Kotlin, this algorithm can be applied to arrays, mutable lists, or immutable lists, making it versatile for many programming tasks. By practicing these different implementations, beginners can build a strong foundation in searching algorithms and prepare for more advanced techniques.




