mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:26:30 +08:00
Add kotlin code for the chapter of sorting (#1145)
* feat(kotlin): add kotlin code for chapter sorting. * style(kotlin): fix some indent. * refactor(kotlin): refactor quick_sort.kt files. * style(kotlin): modified medianThree function.
This commit is contained in:
parent
7f43f92ae9
commit
1845937c5a
9 changed files with 521 additions and 0 deletions
49
codes/kotlin/chapter_sorting/bubble_sort.kt
Normal file
49
codes/kotlin/chapter_sorting/bubble_sort.kt
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* File: bubble_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 冒泡排序 */
|
||||
fun bubbleSort(nums: IntArray) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j] = nums[j+1].also { nums[j+1] = nums[j] }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化) */
|
||||
fun bubbleSortWithFlag(nums: IntArray) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
var flag = false // 初始化标志位
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
nums[j] = nums[j + 1].also { nums[j] = nums[j + 1] }
|
||||
flag = true // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break // 此轮“冒泡”未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
bubbleSort(nums)
|
||||
println("冒泡排序完成后 nums = ${nums.contentToString()}")
|
||||
|
||||
val nums1 = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
bubbleSortWithFlag(nums1)
|
||||
println("冒泡排序完成后 nums1 = ${nums1.contentToString()}")
|
||||
}
|
46
codes/kotlin/chapter_sorting/bucket_sort.kt
Normal file
46
codes/kotlin/chapter_sorting/bucket_sort.kt
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* File: bucket_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
/* 桶排序 */
|
||||
fun bucketSort(nums: FloatArray) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
val k = nums.size / 2
|
||||
val buckets = ArrayList<ArrayList<Float>>()
|
||||
for (i in 0..<k) {
|
||||
buckets.add(ArrayList())
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
for (num in nums) {
|
||||
// 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
val i = (num * k).toInt()
|
||||
// 将 num 添加进桶 i
|
||||
buckets[i].add(num)
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
for (bucket in buckets) {
|
||||
// 使用内置排序函数,也可以替换成其他排序算法
|
||||
bucket.sort()
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
var i = 0
|
||||
for (bucket in buckets) {
|
||||
for (num in bucket) {
|
||||
nums[i++] = num
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
val nums = floatArrayOf(0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f)
|
||||
bucketSort(nums)
|
||||
println("桶排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
80
codes/kotlin/chapter_sorting/counting_sort.kt
Normal file
80
codes/kotlin/chapter_sorting/counting_sort.kt
Normal file
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* File: counting_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
import kotlin.math.max
|
||||
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
fun countingSortNaive(nums: IntArray) {
|
||||
// 1. 统计数组最大元素 m
|
||||
var m = 0
|
||||
for (num in nums) {
|
||||
m = max(m.toDouble(), num.toDouble()).toInt()
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
val counter = IntArray(m + 1)
|
||||
for (num in nums) {
|
||||
counter[num]++
|
||||
}
|
||||
// 3. 遍历 counter ,将各元素填入原数组 nums
|
||||
var i = 0
|
||||
for (num in 0..<m + 1) {
|
||||
var j = 0
|
||||
while (j < counter[num]) {
|
||||
nums[i] = num
|
||||
j++
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
fun countingSort(nums: IntArray) {
|
||||
// 1. 统计数组最大元素 m
|
||||
var m = 0
|
||||
for (num in nums) {
|
||||
m = max(m.toDouble(), num.toDouble()).toInt()
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
val counter = IntArray(m + 1)
|
||||
for (num in nums) {
|
||||
counter[num]++
|
||||
}
|
||||
// 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
|
||||
// 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
|
||||
for (i in 0..<m) {
|
||||
counter[i + 1] += counter[i]
|
||||
}
|
||||
// 4. 倒序遍历 nums ,将各元素填入结果数组 res
|
||||
// 初始化数组 res 用于记录结果
|
||||
val n = nums.size
|
||||
val res = IntArray(n)
|
||||
for (i in n - 1 downTo 0) {
|
||||
val num = nums[i]
|
||||
res[counter[num] - 1] = num // 将 num 放置到对应索引处
|
||||
counter[num]-- // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
}
|
||||
// 使用结果数组 res 覆盖原数组 nums
|
||||
for (i in 0..<n) {
|
||||
nums[i] = res[i]
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(1, 0, 1, 2, 0, 4, 0, 2, 2, 4)
|
||||
countingSortNaive(nums)
|
||||
println("计数排序(无法排序对象)完成后 nums = ${nums.contentToString()}")
|
||||
|
||||
val nums1 = intArrayOf(1, 0, 1, 2, 0, 4, 0, 2, 2, 4)
|
||||
countingSort(nums1)
|
||||
println("计数排序完成后 nums1 = ${nums1.contentToString()}")
|
||||
}
|
48
codes/kotlin/chapter_sorting/heap_sort.kt
Normal file
48
codes/kotlin/chapter_sorting/heap_sort.kt
Normal file
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* File: heap_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||
fun siftDown(nums: IntArray, n: Int, li: Int) {
|
||||
var i = li
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
val l = 2 * i + 1
|
||||
val r = 2 * i + 2
|
||||
var ma = i
|
||||
if (l < n && nums[l] > nums[ma]) ma = l
|
||||
if (r < n && nums[r] > nums[ma]) ma = r
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
if (ma == i) break
|
||||
// 交换两节点
|
||||
nums[i] = nums[ma].also { nums[ma] = nums[i] }
|
||||
// 循环向下堆化
|
||||
i = ma
|
||||
}
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
fun heapSort(nums: IntArray) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (i in nums.size / 2 - 1 downTo 0) {
|
||||
siftDown(nums, nums.size, i)
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
nums[0] = nums[i].also { nums[i] = nums[0] }
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
heapSort(nums)
|
||||
println("堆排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
29
codes/kotlin/chapter_sorting/insertion_sort.kt
Normal file
29
codes/kotlin/chapter_sorting/insertion_sort.kt
Normal file
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* File: insertion_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 插入排序 */
|
||||
fun insertionSort(nums: IntArray) {
|
||||
//外循环: 已排序元素为 1, 2, ..., n
|
||||
for (i in nums.indices) {
|
||||
val base = nums[i]
|
||||
var j = i - 1
|
||||
// 内循环: 将 base 插入到已排序部分的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位
|
||||
j--
|
||||
}
|
||||
nums[j + 1] = base // 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
insertionSort(nums)
|
||||
println("插入排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
54
codes/kotlin/chapter_sorting/merge_sort.kt
Normal file
54
codes/kotlin/chapter_sorting/merge_sort.kt
Normal file
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* File: merge_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 合并左子数组和右子数组 */
|
||||
fun merge(nums: IntArray, left: Int, mid: Int, right: Int) {
|
||||
// 左子数组区间为 [left, mid], 右子数组区间为 [mid+1, right]
|
||||
// 创建一个临时数组 tmp ,用于存放合并后的结果
|
||||
val tmp = IntArray(right - left + 1)
|
||||
// 初始化左子数组和右子数组的起始索引
|
||||
var i = left
|
||||
var j = mid + 1
|
||||
var k = 0
|
||||
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
|
||||
while (i <= mid && j <= right) {
|
||||
if (nums[i] <= nums[j]) tmp[k++] = nums[i++]
|
||||
else tmp[k++] = nums[j++]
|
||||
}
|
||||
// 将左子数组和右子数组的剩余元素复制到临时数组中
|
||||
while (i <= mid) {
|
||||
tmp[k++] = nums[i++]
|
||||
}
|
||||
while (j <= right) {
|
||||
tmp[k++] = nums[j++]
|
||||
}
|
||||
// 将临时数组 tmp 中的元素复制回原数组 nums 的对应区间
|
||||
for (l in tmp.indices) {
|
||||
nums[left + l] = tmp[l]
|
||||
}
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
fun mergeSort(nums: IntArray, left: Int, right: Int) {
|
||||
// 终止条件
|
||||
if (left >= right) return // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
val mid = (left + right) / 2 // 计算中点
|
||||
mergeSort(nums, left, mid) // 递归左子数组
|
||||
mergeSort(nums, mid + 1, right) // 递归右子数组
|
||||
// 合并阶段
|
||||
merge(nums, left, mid, right)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 归并排序 */
|
||||
val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4)
|
||||
mergeSort(nums, 0, nums.size - 1)
|
||||
println("归并排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
119
codes/kotlin/chapter_sorting/quick_sort.kt
Normal file
119
codes/kotlin/chapter_sorting/quick_sort.kt
Normal file
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
* File: quick_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 元素交换 */
|
||||
fun swap(nums: IntArray, i: Int, j: Int) {
|
||||
nums[i] = nums[j].also { nums[j] = nums[i] }
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
fun partition(nums: IntArray, left: Int, right: Int): Int {
|
||||
// 以 nums[left] 为基准数
|
||||
var i = left
|
||||
var j = right
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j-- // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++ // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j) // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left) // 将基准数交换至两子数组的分界线
|
||||
return i // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序 */
|
||||
fun quickSort(nums: IntArray, left: Int, right: Int) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right) return
|
||||
// 哨兵划分
|
||||
val pivot = partition(nums, left, right)
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1)
|
||||
quickSort(nums, pivot + 1, right)
|
||||
}
|
||||
|
||||
/* 选取三个候选元素的中位数 */
|
||||
fun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {
|
||||
val l = nums[left]
|
||||
val m = nums[mid]
|
||||
val r = nums[right]
|
||||
if ((m in l..r) || (m in r..l))
|
||||
return mid // m 在 l 和 r 之间
|
||||
if ((l in m..r) || (l in r..m))
|
||||
return left // l 在 m 和 r 之间
|
||||
return right
|
||||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
fun partitionMedian(nums: IntArray, left: Int, right: Int): Int {
|
||||
// 选取三个候选元素的中位数
|
||||
val med = medianThree(nums, left, (left + right) / 2, right)
|
||||
// 将中位数交换至数组最左端
|
||||
swap(nums, left, med)
|
||||
// 以 nums[left] 为基准数
|
||||
var i = left
|
||||
var j = right
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j-- // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++ // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j) // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left) // 将基准数交换至两子数组的分界线
|
||||
return i // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序 */
|
||||
fun quickSortMedian(nums: IntArray, left: Int, right: Int) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right) return
|
||||
// 哨兵划分
|
||||
val pivot = partitionMedian(nums, left, right)
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1)
|
||||
quickSort(nums, pivot + 1, right)
|
||||
}
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
fun quickSortTailCall(nums: IntArray, left: Int, right: Int) {
|
||||
// 子数组长度为 1 时终止
|
||||
var l = left
|
||||
var r = right
|
||||
while (l < r) {
|
||||
// 哨兵划分操作
|
||||
val pivot = partition(nums, l, r)
|
||||
// 对两个子数组中较短的那个执行快速排序
|
||||
if (pivot - l < r - pivot) {
|
||||
quickSort(nums, l, pivot - 1) // 递归排序左子数组
|
||||
l = pivot + 1 // 剩余未排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, r) // 递归排序右子数组
|
||||
r = pivot - 1 // 剩余未排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 快速排序 */
|
||||
val nums = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSort(nums, 0, nums.size - 1)
|
||||
println("快速排序完成后 nums = ${nums.contentToString()}")
|
||||
|
||||
/* 快速排序(中位基准数优化) */
|
||||
val nums1 = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSortMedian(nums1, 0, nums1.size - 1)
|
||||
println("快速排序(中位基准数优化)完成后 nums1 = ${nums1.contentToString()}")
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
val nums2 = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSortTailCall(nums2, 0, nums2.size - 1)
|
||||
println("快速排序(尾递归优化)完成后 nums2 = ${nums2.contentToString()}")
|
||||
}
|
67
codes/kotlin/chapter_sorting/radix_sort.kt
Normal file
67
codes/kotlin/chapter_sorting/radix_sort.kt
Normal file
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* File: radix_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
|
||||
fun digit(num: Int, exp: Int): Int {
|
||||
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
|
||||
return (num / exp) % 10
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
fun countingSortDigit(nums: IntArray, exp: Int) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
|
||||
val counter = IntArray(10)
|
||||
val n = nums.size
|
||||
// 统计 0~9 各数字的出现次数
|
||||
for (i in 0..<n) {
|
||||
val d = digit(nums[i], exp) // 获取 nums[i] 第 k 位,记为 d
|
||||
counter[d]++ // 统计数字 d 的出现次数
|
||||
}
|
||||
// 求前缀和,将“出现个数”转换为“数组索引”
|
||||
for (i in 1..9) {
|
||||
counter[i] += counter[i - 1]
|
||||
}
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入 res
|
||||
val res = IntArray(n)
|
||||
for (i in n - 1 downTo 0) {
|
||||
val d = digit(nums[i], exp)
|
||||
val j = counter[d] - 1 // 获取 d 在数组中的索引 j
|
||||
res[j] = nums[i] // 将当前元素填入索引 j
|
||||
counter[d]-- // 将 d 的数量减 1
|
||||
}
|
||||
// 使用结果覆盖原数组 nums
|
||||
for (i in 0..<n) nums[i] = res[i]
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
fun radixSort(nums: IntArray) {
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
var m = Int.MIN_VALUE
|
||||
for (num in nums) if (num > m) m = num
|
||||
var exp = 1
|
||||
// 按照从低位到高位的顺序遍历
|
||||
while (exp <= m) {
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSortDigit(nums, exp)
|
||||
exp *= 10
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
// 基数排序
|
||||
val nums = intArrayOf(
|
||||
10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996
|
||||
)
|
||||
radixSort(nums)
|
||||
println("基数排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
29
codes/kotlin/chapter_sorting/selection_sort.kt
Normal file
29
codes/kotlin/chapter_sorting/selection_sort.kt
Normal file
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* File: selection_sort.kt
|
||||
* Created Time: 2024-1-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* 选择排序 */
|
||||
fun selectionSort(nums: IntArray) {
|
||||
val n = nums.size
|
||||
// 外循环:未排序区间为 [i, n-1]
|
||||
for (i in 0..<n - 1) {
|
||||
var k = i
|
||||
// 内循环:找到未排序区间内的最小元素
|
||||
for (j in i + 1..<n) {
|
||||
if (nums[j] < nums[k]) k = j // 记录最小元素的索引
|
||||
}
|
||||
// 将该最小元素与未排序区间的首个元素交换
|
||||
nums[i] = nums[k].also { nums[k] = nums[i] }
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
selectionSort(nums)
|
||||
println("选择排序完成后 nums = ${nums.contentToString()}")
|
||||
}
|
Loading…
Reference in a new issue