Select Sort in scala
2015-08-17 18:57
429 查看
Below I written a simple select sort in scala, just for recording something.
object SelectSort { /* Return the index of the minimum element in the given array, index range [left, right] */ def indexMin(a: Array[Int], left: Int, right: Int): Int = { var minVal = a(left); var minIndex = left; for (i <- (left + 1) to right) { if (a(i) < minVal) { minVal = a(i); minIndex = i; } } return minIndex; } /* Swaps the array elements at index i and j */ def swap(a: Array[Int], i: Int, j: Int) { val temp = a(i); a(i) = a(j); a(j) = temp; } /* Sort the given array using select sort */ def selectSort(a: Array[Int]) { val right = a.length - 1; for (left <- 0 until right) { val min = indexMin(a, left, right); if (left != min) { swap(a, left, min) } } } /* Another one */ def selectSort1(a: Array[Int]) { val N = a.length; for (i <- 0 until N - 1) { var min = i; for (j <- i + 1 until N) { if (a(j) < a(min)) { min = j; } } swap(a, i, min); } } /* Output the array */ def output(a: Array[Int]) { for (v <- a) { print(v + " "); } println(); } /* Get random Int number betwen from and until */ def random(from: Int, until: Int): Int = { val size = until - from; return from + (math.random * size).toInt; } /* Get the specified random numbers */ def getRandomNum(n: Int, from: Int, until: Int): Array[Int] = { val elements:Array[Int] = new Array[Int](n); for (i <- 0 until n) { elements(i) = random(from, until); } elements; } /* Randomly permutes the given array's elements */ def permute(a: Array[Int]) { val n = a.length; for (i <- 0 until (n - 1)) { val j = random(i, n); if (i != j) { swap(a, i, j); } } } // Doing the test /* val n = 10; val elements:Array[Int] = new Array[Int](n); for (i <- 0 until n) { elements(i) = i + 1; } permute(elements);*/ val elements = getRandomNum(10, 0, 100); print("Before select sort: "); output(elements); print("After select sort: "); selectSort1(elements); output(elements); }
相关文章推荐
- 菜鸟成长日记-coalesce函数&&collate&&memcache缓存
- Java中Integer直接比较大小
- 菜鸟成长日记-coalesce函数&&collate&&memcache缓存
- POJ 1436 Horizontally Visible Segments (线段树水过)
- 8.10~8.15周题库
- 幸好有这个,不然七夕本汪又被虐了!
- hdu1717小数化分数2
- Leetcode 58 Length of Last Word
- Java序列化及反序列化 示例讲解
- SettingsProvider 它SettingsCache
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- hdu 5381 莫队算法/gcd
- [dfs]poj2718 Smallest Difference
- 如何绘制虚线
- php&&memcache
- php&&memcache
- Hive视图
- php&&页面静态化
- linux 下常用命令
- Android开发环境搭建与HelloWorld