JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Languages on JVM » Scala  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 Scala 的選擇、插入、氣泡、快速排序
caterpillar

良葛格

版主

發文: 2613
積分: 70
於 2009-12-16 12:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
無聊貼一下。。XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
object Sort {
    def selection(xs: List[Int]): List[Int] = {
        if(xs.isEmpty) Nil
        else select(xs)
    }
    private def select(xs: List[Int]): List[Int] = {
        val min = xs.reduceLeft((m, k) => if(m > k) k else m)
        val remain = xs.filter(_ != min)
        if(remain.isEmpty) xs
        else xs.filter(_ == min) ++ select(remain)           
    }
    
    def insertion(xs: List[Int]): List[Int] = {
        if(xs.isEmpty) Nil
        else insert(xs.head, insertion(xs.tail))
    }
    private 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 bubble(xs: List[Int]):List[Int] = {
        if(xs.isEmpty) Nil
        else up(xs)
    }
    private def up(xs: List[Int]): List[Int] = {
        if(xs.tail.isEmpty) xs
        else {
            val s = bubble(xs.tail)
            if(xs.head > s.head) s.head :: up(xs.head :: s.tail)
            else xs.head :: s
        }
    }
 
    def quick(list: List[Int]): List[Int] = {
       list match {
           case Nil => Nil     
           case x::xs =>        
           val (before,after) = xs partition (_ < x)
           quick(before) ++ (x :: quick(after))
       }
    }
}


reply to postreply to post
良葛格學習筆記
話題樹型展開
人氣 標題 作者 字數 發文時間
1753 Scala 的選擇、插入、氣泡、快速排序 caterpillar 1324 2009-12-16 12:27
» JWorld@TW »  Languages on JVM » Scala

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8