1、产生随机序

def RandomList(n: Int) = Seq.fill(n)(scala.util.Random.nextInt(n))def RandomDiffList(n: Int): List[Int] = {var resultList: List[Int] = Nilwhile (resultList.length < n) {val tempNum = (new scala.util.Random).nextInt(n)if (!resultList.contains(tempNum))resultList = resultList ::: List(tempNum)}resultList}

2、排序算法

1)希尔排序

def shellSort(SortList: List[Int]): List[Int] = {val cpSortList = SortList.toArrayvar d = cpSortList.lengthwhile (d > 1) {d = math.floor(d / 2).toIntfor (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))minIndex = o}val temp = cpSortList(j)cpSortList(j) = cpSortList(minIndex)cpSortList(minIndex) = temp}}}cpSortList.toList}

2)简单选择排序

def selectSort(SortList: List[Int]): List[Int] = {val cpSortList = SortList.toArrayfor (i <- 0 until cpSortList.length) {var minIndex = ifor (j <- i + 1 until cpSortList.length) {if (cpSortList(minIndex) > cpSortList(j))minIndex = j}val temp = cpSortList(i)cpSortList(i) = cpSortList(minIndex)cpSortList(minIndex) = temp}cpSortList toList}

3)冒泡排序

def bubleSort(SortList: List[Int]): List[Int] = {val cpSortList = SortList.toArrayfor (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}

4)快速排序

 def quickSort(SortList: Array[Int]): Array[Int] = {if (SortList.length <= 1) SortListelse {val pivot = SortList(SortList.length / 2)Array.concat(quickSort(SortList filter (pivot >)),SortList filter (pivot ==),quickSort(SortList filter (pivot <)))}}

5)归并排序

def mergeSort(SortList: List[Int]): List[Int] = {def merge(a: List[Int], b: List[Int]): List[Int] = (a, b) match {case (Nil, _) => bcase (_, Nil) => acase (x :: xs, y :: ys) =>if (x <= y) x :: merge(xs, b)else y :: merge(a, ys)}if (SortList.length == 1) SortListelse {val (first, second) = SortList.splitAt(SortList.length / 2)merge(mergeSort(first), mergeSort(second))}}
}

3、伴生对象

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))}
}

4、排序结果

scala编写排序算法相关推荐

  1. php代码编写直接插入排序算法,PHP排序算法之直接插入排序(Straight Insertion Sort)实例分析...

    本文实例讲述了PHP排序算法之直接插入排序(Straight Insertion Sort).分享给大家供大家参考,具体如下: 算法引入: 在这里我们依然使用<大话数据结构>里面的一个例子 ...

  2. php编写冒泡排序算法_PHP排序算法之冒泡排序(Bubble Sort)实现方法详解

    本文实例讲述了PHP排序算法之冒泡排序(Bubble Sort)实现方法.分享给大家供大家参考,具体如下: 基本思想: 冒泡排序是一种交换排序,它的基本思想是:两两比较相邻记录的关键字,如果反序则交换 ...

  3. 用c语言编写插入排序算法,C语言实现常用排序算法——插入排序

    插入排序是最基础的排序算法,原理: 首先1个元素肯定是有序的,所以插入排序从第二个元素开始遍历: 内循环首先请求一个空间保存待插入元素,从当前元素向数组起始位置反向遍历: 当发现有大于待插入元素的元素 ...

  4. 程序员面试算法_程序员的前20个搜索和排序算法面试问题

    程序员面试算法 大家好,如果您正在准备编程工作面试或正在寻找新工作,那么您知道这不是一个容易的过程. 在您职业的任何阶段,您都必须幸运地接到电话并进行第一轮面试,但是在初学者方面,当您寻找第一份工作时 ...

  5. 程序员的前20个搜索和排序算法面试问题

    大家好,如果您正在准备编程工作面试或正在寻找新工作,那么您知道这不是一个容易的过程. 在您职业的任何阶段,您都必须幸运地接到电话并进行第一轮面试,但是在初学者方面,当您寻找第一份工作时就更加困难. 这 ...

  6. java python算法_用Python,Java和C ++示例解释的排序算法

    java python算法 什么是排序算法? (What is a Sorting Algorithm?) Sorting algorithms are a set of instructions t ...

  7. 数据结构(三) 用java实现七种排序算法。

    很多时候,听别人在讨论快速排序,选择排序,冒泡排序等,都觉得很牛逼,心想,卧槽,排序也分那么多种,就觉得别人很牛逼呀,其实不然,当我们自己去了解学习后发现,并没有想象中那么难,今天就一起总结一下各种排 ...

  8. 读论文之《基于 FPGA 的并行全比较排序算法》

    为什么介绍这种排序算法:最近在独立编写霍夫曼编码的verilog HDL设计程序,其中用到了排序模块,对一组数据进行排序,苦寻几天,最终找到了这个全新的适合硬件实现的排序算法,分享与此,以空间换时间的 ...

  9. 常用的排序算法的时间复杂度和空间复杂度

    常用的排序算法的时间复杂度和空间复杂度                                           1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出 ...

最新文章

  1. linux设备:初始化
  2. Java1.5语言新特性简单总结
  3. 你不知道的JavaScript运算符
  4. 吉林大学计算机学院刘衍衍教授,刘磊-吉林大学计算机科学与技术学院
  5. VTK:PolyData之Curvatures
  6. 还不会用typedef?C语言typedef的详细用法总结,一篇解决你的困惑。(学习笔记2--typedef设置别名)
  7. 【C++实现python字符串函数库】strip、lstrip、rstrip方法
  8. 抽奖滚动文字如何透明地覆盖在动态视频背景上?-活动现场双屏管理系统操作提示
  9. android 平板刷机工具,小白必看,安卓平板电脑刷机教程之一键刷机
  10. 使用VMware创建一个虚拟机,并安装乌班图系统
  11. 最新C语言编程软件推荐(2021整理)
  12. h5 7个移动端框架
  13. Postman测试json格式的post请求
  14. 保护电路:简单的限流保护电路图
  15. 用实际程序来测试udp sendto函数的最大发包大小------为什么是65507?
  16. MSOCache文件夹能否删除
  17. leetcode:448. Find All Numbers Disappeared in an Array找到所有数组中消失的数字(C++)
  18. 海海软件发布动态网站加密(DRM版权保护)Beta版
  19. Redis哨兵原理详解
  20. 幼儿园体育游戏电子计算机教案,【小班体育活动】_小班体育教案100篇_小班体育游戏活动教案5篇...

热门文章

  1. 计算机如何理解我们的语言?NLP is fun!
  2. 用AI为美国政府“助纣为虐”,微软员工怒了
  3. 如何设计一颗40PFLOPS量级的AI芯片?
  4. 一文读懂AlphaGo背后的强化学习:它的背景知识与贝尔曼方程的原理
  5. 别再写 main 方法测试了,太 Low!这才是专业 Java 测试方法!
  6. 了解这些,你就可以在Spring启动时为所欲为了
  7. 你还在用Swagger?试试这个神器!
  8. 【廖雪峰python入门笔记】变量
  9. 测试集没有标签,应该怎么办?
  10. 蚂蚁金服大佬自述:保持学习力,永远胜过不切实际的一夜暴富幻想!