Scala : Check Sort using anonymous function

object issorted extends App{

def issorted[A](as:Array[A],ordered:(A,A)=>Boolean):Boolean=
{
def loop(n:Int): Boolean =
{
if (n<=0) true // if recursion goes to first element then array is sorted
else ordered(as(n),as(n-1)) && loop(n-1) // else check the and && conditions for oredred function and loop(n-1) .. bith true then true else false
}
loop(as.length-1)
}
print(issorted(Array(1,2,3,4,5),(x:Int,y:Int)=>x>y)) // declare anonymous function
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s