Skip to content

Commit

Permalink
linear search implemented recursively in scala
Browse files Browse the repository at this point in the history
  • Loading branch information
manikTharaka committed Oct 4, 2017
1 parent 3ba2555 commit b47bfe7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions search/linear_search/scala/linear_search.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Recursive linear search implemented in scala
* unnecessarily complex
*/
def linear_search(lst:Array[Int],idx:Int,key:Int):Int={
if(lst.length == 0 ) return -1 else
if(lst(idx) == key) return idx else
return linear_search(lst,idx+1,key)
}

def test(){

val l = Array(12,3,4,5,6,7)

assert(linear_search(l,0,4) == 2)
}

0 comments on commit b47bfe7

Please sign in to comment.