selectSortApp { def main(args: Array[String]): Unit = { val list: ListBuffer[Int] = ListBuffer(1, 2, 3, 10, 100, 19999, -1998, 9, 234, 234, 9, 43) val res: ListBuffer[Int] = Sec_Sort[Int](_<_)(list) println(res.mkString(",")) } /** * 选择排序,每次选择一个最小的值 * @param list * @return */ def SelectSort(list: ListBuffer[Int]): ListBuffer[Int] = { var minIndex = 0 var temp = 0 for(i<- 0 until(list.size)){ minIndex = i for(j <- i+1 until(list.size)){ if(list(j)< list(minIndex)){ minIndex = j } } temp = list(i) list(i) = list(minIndex) list(minIndex) = temp } list } /** *假定第一数据为有序区域,其余为无序区域。每一趟从无需区域找到对应的最大/最小, 然后和有序区域交换,直到最后序列变成一个有序区域。 * 属于不稳定排序 * * @param comparator * @param lists * @tparam T * @return */ def Sec_Sort[T](comparator:(T,T)=>Boolean)(lists:ListBuffer[T]):ListBuffer[T]={ for(i<-0 until lists.length){ var min=lists(i) //定义最小的 var index=i; // 最小的索引 for(j<-i+1 until lists.length){ if(comparator(lists(j), min)){ min=lists(j) //每一趟找到最小的值和对应的索引 index=j } } //替换最小值 if(index!=i){ var temp =lists(i) lists(i) = lists(index) lists(index) = temp } } lists //最后一行返回排好的序列 }} 继续阅读与本文标签相同的文章
上一篇 :
scala 二分法查找
下一篇 :
sclaa -快速排序
-
如何简洁实现游戏中的AI
2026-05-25栏目: 教程
-
如何快速搭建一个简单的塔防小游戏
2026-05-25栏目: 教程
-
一个小小的C++游戏引擎
2026-05-25栏目: 教程
-
你的Redis有类转换异常么
2026-05-25栏目: 教程
-
Redis 基础数据结构
2026-05-25栏目: 教程
