Welcome to BigData School that can get you hired on Small startups or big Product based companies.Are you tired of 30-40 hours of theoritical bigdata courses in the market. Welcome to 240+ hours of Bigdata training. Just subscribe to our popup to get access to our 80+hours of course absolutely free.Try them out and you can join our further course once you are happy with our Demos.

Linear Search Using Scala

Big Data    On Thursday 22nd of June 2017 12:10:06 PM By Suraz Ghimire
The below program will do a linear search using scala.
We use scala ,mostly for Spark programming which is a bigdata technology. Some other big data technologies are hadoop,pig,hive,sqoop etc.

Lets see the algorithm quickly.
 

package algorithm2020

object _1LinearSearch {

def search(arr:Array[Int],num: Int):Int= {
for(i<- 0 until arr.length){
if(arr(i)==num)
return i //return keyword mandatory here
}
-1
}

def main(args: Array[String]): Unit = {
val arr=Array(1,6,3,8,9)
val num=10
val pos=search(arr,num)
pos match{
case -1=>println("Not Found")
case n=> println(s"$num found at index:$pos")
}

}
}

Time complexity: O(n) because it has to search linearly.
Space complexity: 1 , as it need just 1 variable to hold the index.

About Author

Suraz Ghimire