选择排序泛型类型实现 [英] Selection Sort Generic type implementation

查看:38
本文介绍了选择排序泛型类型实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照自己的方式实现了递归版本的选择和快速排序,我试图修改代码,使其可以对任何泛型类型的列表进行排序,我想假设提供的泛型类型可以转换可在运行时进行比较.

I worked my way implementing a recursive version of selection and quick sort,i am trying to modify the code in a way that it can sort a list of any generic type , i want to assume that the generic type supplied can be converted to Comparable at runtime.

有没有人有关于如何做到这一点的链接、代码或教程我正在尝试修改此特定代码

Does anyone have a link ,code or tutorial on how to do this please I am trying to modify this particular code

  'def main (args:Array[String]){
    val l = List(2,4,5,6,8)
    print(quickSort(l))
  }
  def quickSort(x:List[Int]):List[Int]={
    x match{
      case xh::xt =>
        {
        val (first,pivot,second) = partition(x)
        quickSort (first):::(pivot :: quickSort(second))
    }
    case Nil => {x}
  }
  }
  def partition (x:List[Int])=
  {
   val pivot =x.head
   var first:List[Int]=List ()
   var second : List[Int]=List ()

   val fun=(i:Int)=> {
     if (i<pivot)
       first=i::first
      else
         second=i::second
   } 
     x.tail.foreach(fun)
     (first,pivot,second)
   }


    enter code here

    def main (args:Array[String]){
    val l = List(2,4,5,6,8)
    print(quickSort(l))
  }
  def quickSort(x:List[Int]):List[Int]={
    x match{
      case xh::xt =>
        {
        val (first,pivot,second) = partition(x)
        quickSort (first):::(pivot :: quickSort(second))
    }
    case Nil => {x}
  }
  }
  def partition (x:List[Int])=
  {
   val pivot =x.head
   var first:List[Int]=List ()
   var second : List[Int]=List ()

   val fun=(i:Int)=> {
     if (i<pivot)
       first=i::first
      else
         second=i::second
   } 
     x.tail.foreach(fun)
     (first,pivot,second)
   } '

语言:SCALA

推荐答案

在 Scala 中,Java Comparator 被替换为 Ordering(非常相似,但有更多有用的方法).它们针对多种类型(原语、字符串、bigDecimals 等)实现,您可以提供自己的实现.

In Scala, Java Comparator is replaced by Ordering (quite similar but comes with more useful methods). They are implemented for several types (primitives, strings, bigDecimals, etc.) and you can provide your own implementations.

然后您可以使用 scala implicit 要求编译器为您选择正确的:

You can then use scala implicit to ask the compiler to pick the correct one for you:

def sort[A]( lst: List[A] )( implicit ord: Ordering[A] ) = {
  ...
}

如果您使用预定义的排序,只需调用:

If you are using a predefined ordering, just call:

sort( myLst )

并且编译器会推断出第二个参数.如果要声明自己的顺序,请在声明中使用关键字 implicit.例如:

and the compiler will infer the second argument. If you want to declare your own ordering, use the keyword implicit in the declaration. For instance:

implicit val fooOrdering = new Ordering[Foo] {
  def compare( f1: Foo, f2: Foo ) = {...}
}

如果您尝试对 Foo 列表进行排序,它将被隐式使用.

and it will be implicitly use if you try to sort a List of Foo.

如果你有多个相同类型的实现,你也可以显式传递正确的排序对象:

If you have several implementations for the same type, you can also explicitly pass the correct ordering object:

sort( myFooLst )( fooOrdering )

这篇文章中的更多信息.

这篇关于选择排序泛型类型实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆