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<String>) {

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: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

}

}

}

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.

Also Read : Bubble Sort Algorithm Program in Kotlin

Also Read : Bubble Sort Algorithm Program in Kotlin

## 0 Comments