Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Linear Search Algorithm Program in Kotlin

In this article, we will learn to write a Kotlin program to sort an array using Linear Search Algorithm.

The time complexity of a Linear Search algorithm is O(n). Linear search is not much efficient compared to binary search algorithm and hash tables. Which allows significantly faster.

Source Code:
fun main(args: Array) {
    val a = intArrayOf(3, 5, -2, 1, -3, 2, -1, -5, -4, 4)
    val target = 2

    for (i in a.indices) {
        if (a[i] == target) {
            println("Element found at index $i")
            break
        }
    }
}
Output:
Element found at index 5
Description:
In this program, we have used a linear search algorithm to search element in the array. For that, we have used for loop to compare the search element with each element of array elements.


This post first appeared on Ask For Program, please read the originial post: here

Share the post

Linear Search Algorithm Program in Kotlin

×

Subscribe to Ask For Program

Get updates delivered right to your inbox!

Thank you for your subscription

×