package Sort /** * Created by legotime on 2016/5/14. */ class ScalaSort { //------------------------产生随机序列------------------------------------------ //产生一组随机数,内部元素可能相同 def RandomList(n:Int) = Seq.fill(n)(scala.util.Random.nextInt(n)) //产生一组随机数,内部元素不相同 def RandomDiffList(n:Int): List[Int] ={ var resultList:List[Int] = Nil while(resultList.length<n){ val tempNum = (new scala.util.Random).nextInt(n) if(! resultList.contains(tempNum)) resultList = resultList:::List(tempNum) } resultList } //-----------------------------排序算法----------------------------------------- //implicit def ListToArray(List1:List[Int]) = List1 toArray //implicit def ArrayToList(Array2:Array[Int]) = Array2 toList //直接插入排序(Straight Select Sorting) //scala 中有直接的insert函数,和直接插入排序的思想一样,在此省略改插入法 //希尔排序(shell sort) def shellSort(SortList:List[Int]):List[Int] ={ val cpSortList = SortList.toArray var d = cpSortList.length while( d > 1){ d = math.floor(d/2).toInt for(i <- 0 to d){ //小组内排好序 for (j <- Range(i,cpSortList.length,d)){ var minIndex = j for (o <- Range(j+d,cpSortList.length,d)){ if(cpSortList(minIndex) > cpSortList(o))//cpSortList(minIndex) < cpSortList(j) 从大到小 minIndex = o } val temp = cpSortList(j) cpSortList(j) = cpSortList(minIndex) cpSortList(minIndex) = temp } } } cpSortList.toList } //简单选择排序(Selection sort) def selectSort(SortList:List[Int]):List[Int] ={ val cpSortList = SortList.toArray for (i <- 0 until cpSortList.length){ var minIndex = i //找到当前循环的最小值 for (j <- i+1 until cpSortList.length){ if(cpSortList(minIndex) > cpSortList(j))//cpSortList(minIndex) < cpSortList(j) 从大到小 minIndex = j } val temp = cpSortList(i) cpSortList(i) = cpSortList(minIndex) cpSortList(minIndex) = temp } cpSortList toList } //冒泡排序(Bubble Sort) def bubleSort(SortList:List[Int]):List[Int] ={ val cpSortList = SortList.toArray for(i <- 0 until cpSortList.length-1; j <- 0 until cpSortList.length-1-i){ if(cpSortList(j) > cpSortList(j+1)){ val tmp = cpSortList(j) cpSortList(j) = cpSortList(j+1) cpSortList(j+1) = tmp } } cpSortList toList } //快速排序把(quickSort) //《Scala By Example》的一个列子 def quickSort(SortList: Array[Int]): Array[Int] = { if(SortList.length <= 1) SortList else{ val pivot = SortList(SortList.length / 2) Array.concat( quickSort(SortList filter (pivot >)), SortList filter (pivot ==), quickSort(SortList filter (pivot <)) ) } } //归并排序 def mergeSort(SortList: List[Int]): List[Int] = { def merge(a: List[Int], b: List[Int]): List[Int] = (a,b) match { case (Nil, _) => b case (_, Nil) => a case (x::xs, y::ys) => if(x <= y) x :: merge(xs, b) else y :: merge(a, ys) } if(SortList.length == 1) SortList else{ val (first, second) = SortList.splitAt(SortList.length/2) merge(mergeSort(first), mergeSort(second)) } } } object ScalaSort { def main(args: Array[String]) { val randomSort = new ScalaSort() val rdmList = randomSort RandomDiffList(10) //val rdmNum = scala.util.Random.nextInt(10) println("排序前:"+rdmList) println("直接插入排序:"+randomSort.selectSort(rdmList)) println("希尔排序:"+randomSort.shellSort(rdmList)) println("冒泡排序:"+randomSort.bubleSort(rdmList)) println("快速排序:"+randomSort.quickSort(rdmList.toArray).toList) println("归并排序:"+randomSort.mergeSort(rdmList)) //排序前:List(3, 9, 8, 7, 0, 1, 4, 5, 6, 2) //直接插入排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) //希尔排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) //冒泡排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) //快速排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) //归并排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) } }
scala实现几大排序算法
最新推荐文章于 2021-02-24 15:21:34 发布