Scala練習-基数ソート

5556 ワード

ベースソートソース
package day14

import scala.collection.mutable.ArrayBuffer

/**
  * Created by doctorq on 2017/6/29.
  *     
  */
object RadixSort extends Utils with App {

  /*
            
   */
  def max(unSort: Array[Int]): Int = {
    if (unSort.size < 1) throw new IllegalArgumentException
    var temp = unSort(0)
    for (i 1 until unSort.size) {
      if (unSort(i) > temp) {
        temp = unSort(i)
      }
    }

    temp
  }

  /**
    *        
    *
    * @param item
    * @return
    */
  def getDigit(item: Int): Int = {
    var digit = 1
    var temp = item / 10
    while (temp > 0) {
      digit += 1
      temp /= 10
    }
    digit
  }

  /**
    *       index    ,123     2,    3,    1
    *
    * @param item
    * @param index
    * @return
    */
  def getNum(item: Int, index: Int): Int = {
    if (index < 1) throw new IllegalArgumentException
    //  ,   0
    if (getDigit(item) < index) return 0
    val itemStr = item.toString
    itemStr.charAt(itemStr.size - index).toString.toInt
  }


  def generate(num: Int): Array[ArrayBuffer[Int]] = {
    val bucket = new Array[ArrayBuffer[Int]](10)
    for (i 0 until num) {
      val temp = new ArrayBuffer[Int]()
      bucket(i) = temp
    }
    bucket
  }

  /**
    *          
    *
    * @param bucketArray
    * @return
    */
  def flatten(bucketArray: Array[ArrayBuffer[Int]], size: Int): Array[Int] = {
    val temp = new Array[Int](size)
    var index = 0
    for (i 0 until bucketArray.size) {
      if (bucketArray(i).size != 0) {
        for (j 0 until bucketArray(i).size if (bucketArray(i)(j) >= 0)) {
          temp(index) = bucketArray(i)(j)
          index += 1
        }
      }
    }
    temp
  }

  /**
    *
    * @param unSort     
    * @param digit        
    * @return        
    */
  def sort(unSort: Array[Int], digit: Int): Array[Int] = {


    var tempUnSort = unSort
    for (i 1 to digit) {
      val bucket = generate(10)
      for (j 0 until tempUnSort.size) {
        val index = getNum(tempUnSort(j), i)
        bucket(index).append(tempUnSort(j))
      }

      tempUnSort = flatten(bucket, tempUnSort.size)
    }

    tempUnSort
  }

  val array = Array[Int](1, 45, 23, 0, 23, 234, 23, 5, 6, 7, 9, 9, 9, 456)
  val digit = getDigit(max(array))
  printlnArray(sort(array, digit))
}