forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_Search.kt
38 lines (36 loc) · 965 Bytes
/
Linear_Search.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Function For Linear Search
fun Linear_Search(list: List<Int>, Num: Int):Int {
//If number not found than return -1 otherwise return postion of that number.
var foundAt:Int = -1
for(number in 0 until list.size) {
if(list[number] == Num) {
foundAt = number
}
}
return foundAt
}
//Driver Function For Linear Search
fun main(args:Array<String>) {
val list = listOf(2, 7, 10, 45, 60, 5)
val position: Int
//Let 45 to be searched
position = Linear_Search(list, 45)
if(position == -1) {
println("Number Not Found")
}
else {
println("Number Found at position ${position + 1}")
}
//Now search for 1
val position1: Int
position1 = Linear_Search(list, 1)
if(position1 == -1) {
println("Number Not Found")
}
else {
println("Number Found at position ${position1 + 1}")
}
}
//Output:-
// Number Found at position 4
// Number Not Found