How to check if list [Int] is sorted in scala?

I would like to know if any isSorted() function isSorted() or not in scala.

Question: check if List[Int] sorted or not, if you do not delete the smallest number and do it again until List[Int] is sorted?

I need only 1 or 2 linear program.

+9
source share
13 answers

You can compare each pair in the input sequence for lists containing more than 1 element:

 def isSorted[T](s: Seq[T])(implicit ord: Ordering[T]): Boolean = s match { case Seq() => true case Seq(_) => true case _ => s.sliding(2).forall { case Seq(x, y) => ord.lteq(x, y) } } 
+14
source

This is not the best solution, but you can use the sorted method in the list and then compare it with the original one.

 def sorted(l: List[Int]): Boolean = l == l.sorted 
+10
source

With some laziness:

 def isSorted(l:List[Int]):Boolean = { val list = l.view !list.zip(list.tail).exists {case (x,y) => x>y} } 
+5
source

Sorting to check if the list is already sorted a bit is redundant. The optimal solution here seems the most obvious, which simply describes the problem in human language and passes it to the code:

 def isSorted[T](list: List[T])(implicit ord: Ordering[T]): Boolean = list match { case Nil => true // an empty list is sorted case x :: Nil => true // a single-element list is sorted case x :: xs => ord.lteq(x, xs.head) && isSorted(xs) // if the first two elements are ordered and the rest are sorted, the full list is sorted too } 

If you want it to be shorter, you could exchange the 2nd case for a little readability:

 def isSorted[T](list: List[T])(implicit ord: Ordering[T]): Boolean = list match { case Nil => true case x :: xs => xs.headOption.fold(true)(ord.lteq(x, _)) && isSorted(xs) } 

If you want to use a single line font, it will be impossible to read:

 def isSorted[T](list: List[T])(implicit ord: Ordering[T]): Boolean = list.headOption.fold(true)(a => list.tail.headOption.fold(true)(ord.lteq(a, _) && isSorted(list.tail.tail))) 
+2
source

Ineffective but clear answer:

 def specialSort(a: List[Int]): List[Int] = if (a == a.sorted) a else specialSort(a.filterNot(_ == a.min)) 
+1
source

l == l.sorted didn’t work for me, I managed to do it with l sameElements l.sorted

+1
source

Here is your one-liner home solution

 def removeMinWhileNotSorted[A: Ordering](xs: List[A]): List[A] = if (xs == xs.sorted) xs else xs.splitAt(xs.indexOf(xs.min)) match {case (prefix, m :: postfix) => removeMinWhileNotSorted(prefix ++ postfix)} 
0
source

He does not exist. But this is easy to do: create a list with a merged version of the list and the same list that you want to sort, and make sure that both elements of the combined list are the same.

Something like that:

 import org.junit.Assert._ val sortedList = List(1, 3, 5, 7) val unsortedList = List(10, 1, 8, 3, 5, 5, 2, 9) // detailed test. It passes. sortedList .zip(sortedList.sortWith((a,b) => a.compareTo(b) < 0)) // this is the required sorting criteria. .foreach(x => assertEquals("collection is not sorted", x._1, x._2)) // easier to read but similar test. It fails. unsortedList .zip(unsortedList.sorted) // this is the required sorting criteria. .foreach(x => assertEquals("collection is not sorted", x._1, x._2)) 

may be a function:

 def isSorted(list: List[Int]): Boolean = !list.zip(list.sortWith((a, b) => a.compareTo(b) < 0)).exists(p => !p._1.equals(p._2)) 
0
source
 def isSorted[T <% Ordered[T]](list: List[T]): Boolean = list.sliding(2).forall(p => (p.size==1) || p(0) < p(1)) 
0
source

I suggested that if two adjacent elements are equal, this is also legal.

  def isSorted[T <% Ordered[T]](l: List[T]):Boolean ={ val a = l.toArray (1 until a.length).forall(i => a(i-1) <= a(i)) } 
0
source

Another opportunity (not necessarily better than some other suggestions)

 def isSorted[T <% Ordered[T]](a: List[T]): Boolean = if (a == Nil) true // an empty list is sorted else a.foldLeft((true, a.head))( (prev, v1) => { val (p, v0) = prev (p && v0 <= v1, v1) })._1 

The results of several test cases:

 isSorted(Nil) -> true isSorted(1 :: Nil) -> true isSorted(2 :: 3 :: Nil) -> true isSorted(1 :: 2 :: 5 :: 8 :: Nil) -> true isSorted(1 :: 1 :: 2 :: 2 :: Nil) -> true isSorted(3 :: 2 :: Nil) -> false isSorted(1 :: 2 :: 3 :: 1 :: Nil) -> false 
0
source

def isSorted (xs: List [Int]): Boolean = (xs.tail zip xs) .forall (pair => pair._1 - pair._2> 0)

0
source

You can use tail recursion to create less objects and avoid for long lists. This version is lazy, the function returns a value immediately after the first unordered pair.

 @scala.annotation.tailrec def isSorted[T : Ordering](values: List[T]): Boolean = { import scala.math.Ordering.Implicits._ values match { case fst :: snd :: _ if fst <= snd => isSorted(values.tail) case _ :: _ :: _ => false case _ => true } } 
0
source

Source: https://habr.com/ru/post/1234463/


All Articles