插入排序在我用Scala编写的代码中不起作用

插入排序在我用Scala编写的代码中不起作用,scala,insertion-sort,Scala,Insertion Sort,我正在编写一个函数来执行插入排序。在编写代码时,我再次得到与输出相同的列表 def insertionSort(xs: List[Int]): List[Int] = { if (xs.isEmpty) Nil else insert(xs.head, xs.tail) } def insert(x: Int, xs: List[Int]): List[Int] = { if (xs.isEmpty || x <= xs.head) x :: xs else xs.hea

我正在编写一个函数来执行插入排序。在编写代码时,我再次得到与输出相同的列表

def insertionSort(xs: List[Int]): List[Int] =
{
  if (xs.isEmpty) Nil
  else insert(xs.head, xs.tail)
}

def insert(x: Int, xs: List[Int]): List[Int] =
{
  if (xs.isEmpty || x <= xs.head) x :: xs
  else xs.head :: insert(x, xs.tail)
}
def insertionSort(xs:List[Int]):List[Int]=
{
如果(xs.isEmpty)Nil
else插入(xs.head,xs.tail)
}
def insert(x:Int,xs:List[Int]):List[Int]=
{

如果(xs.isEmpty | | x我猜您的函数中缺少一个小的递归调用。请参考下面的代码

def insertionSort(xs: List[Int]): List[Int] =
{
  if (xs.isEmpty) Nil
  else insert(xs.head, insertionSort(xs.tail))
}


def insert(x: Int, xs: List[Int]): List[Int] =
{
  if (xs.isEmpty || x <= xs.head) x :: xs
  else xs.head :: insert(x, xs.tail)
}
def insertionSort(xs:List[Int]):List[Int]=
{
如果(xs.isEmpty)Nil
else插入(xs.head,insertionSort(xs.tail))
}
def insert(x:Int,xs:List[Int]):List[Int]=
{

如果(xs.isEmpty | | x类似于
/*这里-->*/insertionSort(xs.tail)/*无论如何。我已经完全删除了它@andreytyukin的可能的副本