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                           //最后一行返回排好的序列  }}
收藏 打印