Scala implement Comb Sort

/**
      *
      * @param arr 待排序的数据
      * @param o   隐式转换
      * @tparam T type
      * @return
      */
    def combSort[T](arr: Array[T])(implicit o: Ordering[T]): (Seq[T], Int) = {
      import o._
      var gap: Int = arr.length
      var hasChanged = true
      var count: Int = 0

      while (gap > 1 || hasChanged) {
        if (gap > 1) gap = (gap * 0.7).toInt

        hasChanged = false

        val consecutiveIndices = (arr.indices, arr.indices drop gap).zipped
        do {
          hasChanged = false
          consecutiveIndices foreach { (i1, i2) =>
            if (arr(i1) > arr(i2)) {
              hasChanged = true
              val tmp = arr(i1)
              arr(i1) = arr(i2)
              arr(i2) = tmp
              count += 1
              println(arr.toList)
            }
          }
        } while (hasChanged)
      }
      (arr, count)
    }



你可能感兴趣的:(Scala implement Comb Sort)