Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Create LinearSearch.kt #279

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Kotlin/LinearSearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.Scanner

fun linearSearch(arr: Array<Int>, key: Int): Int {
for (i in arr.indices)
{
if (arr[i] == key) {
return i
}
}
return -1
}

fun main() {
val input = Scanner(System.`in`)

val arr = arrayOf(1, 2, 3, 4, 5)
val key = input.nextInt();

val index = linearSearch(arr, key)

if(index == -1 ){
print("The Number Not Found")
}
else {
print("The Number found at position : ${index}")
}
}

/*

Sample Array : 1 2 3 4 5

case : 1

stdin:5
The Number found at position : 4

case : 2

stdin:1
The Number found at position : 0

case : 3

stdin:9
The Number Not Found

Space Complexity : O(1)

Best Case Complexity : O(1)
Worst Case Complexity : O(n)
*/