Refine kotlin code (#1241)

* style(kotlin): Improve kotlin codes readability.

* remove redundant quotes.

* style(kotlin): improve codes readability.

* style(kotlin): refine kotlin codes.

* Create kotlin.yml

* Create kotlin.yml

* Delete .github/workflows/kotlin

* Delete .github/workflows/main.yml

* Create kotlin.yml

* Update kotlin.yml

* Delete .github/workflows/kotlin.yml

* Create hello_world_workflow.main.kts

* Delete .github/workflows/hello_world_workflow.main.kts

* remove empty line
This commit is contained in:
curtishd
2024-04-09 16:26:58 +08:00
committed by GitHub
parent 41dd677338
commit 896d9a64f6
22 changed files with 158 additions and 112 deletions

View File

@@ -14,7 +14,7 @@ fun bubbleSort(nums: IntArray) {
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] }
nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }
}
}
}

View File

@@ -6,15 +6,13 @@
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>>()
val buckets = mutableListOf<MutableList<Float>>()
for (i in 0..<k) {
buckets.add(ArrayList())
buckets.add(mutableListOf())
}
// 1. 将数组元素分配到各个桶中
for (num in nums) {

View File

@@ -14,7 +14,7 @@ fun countingSortNaive(nums: IntArray) {
// 1. 统计数组最大元素 m
var m = 0
for (num in nums) {
m = max(m.toDouble(), num.toDouble()).toInt()
m = max(m, num)
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
@@ -40,7 +40,7 @@ fun countingSort(nums: IntArray) {
// 1. 统计数组最大元素 m
var m = 0
for (num in nums) {
m = max(m.toDouble(), num.toDouble()).toInt()
m = max(m, num)
}
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数

View File

@@ -14,10 +14,13 @@ fun siftDown(nums: IntArray, n: Int, li: Int) {
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
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
if (ma == i)
break
// 交换两节点
nums[i] = nums[ma].also { nums[ma] = nums[i] }
// 循环向下堆化
@@ -45,4 +48,4 @@ fun main() {
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
heapSort(nums)
println("堆排序完成后 nums = ${nums.contentToString()}")
}
}

View File

@@ -12,7 +12,7 @@ fun insertionSort(nums: IntArray) {
for (i in nums.indices) {
val base = nums[i]
var j = i - 1
// 内循环: 将 base 插入到已排序部分的正确位置
// 内循环将 base 插入到已排序区间 [0, i-1] 中的正确位置
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位
j--
@@ -26,4 +26,4 @@ fun main() {
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
insertionSort(nums)
println("插入排序完成后 nums = ${nums.contentToString()}")
}
}

View File

@@ -17,8 +17,10 @@ fun merge(nums: IntArray, left: Int, mid: Int, right: Int) {
var k = 0
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) tmp[k++] = nums[i++]
else tmp[k++] = nums[j++]
if (nums[i] <= nums[j])
tmp[k++] = nums[i++]
else
tmp[k++] = nums[j++]
}
// 将左子数组和右子数组的剩余元素复制到临时数组中
while (i <= mid) {
@@ -51,4 +53,4 @@ fun main() {
val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4)
mergeSort(nums, 0, nums.size - 1)
println("归并排序完成后 nums = ${nums.contentToString()}")
}
}

View File

@@ -20,7 +20,7 @@ fun countingSortDigit(nums: IntArray, exp: Int) {
// 统计 0~9 各数字的出现次数
for (i in 0..<n) {
val d = digit(nums[i], exp) // 获取 nums[i] 第 k 位,记为 d
counter[d]++ // 统计数字 d 的出现次数
counter[d]++ // 统计数字 d 的出现次数
}
// 求前缀和,将“出现个数”转换为“数组索引”
for (i in 1..9) {
@@ -31,11 +31,12 @@ fun countingSortDigit(nums: IntArray, exp: Int) {
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
res[j] = nums[i] // 将当前元素填入索引 j
counter[d]-- // 将 d 的数量减 1
}
// 使用结果覆盖原数组 nums
for (i in 0..<n) nums[i] = res[i]
for (i in 0..<n)
nums[i] = res[i]
}
/* 基数排序 */

View File

@@ -14,7 +14,8 @@ fun selectionSort(nums: IntArray) {
var k = i
// 内循环:找到未排序区间内的最小元素
for (j in i + 1..<n) {
if (nums[j] < nums[k]) k = j // 记录最小元素的索引
if (nums[j] < nums[k])
k = j // 记录最小元素的索引
}
// 将该最小元素与未排序区间的首个元素交换
nums[i] = nums[k].also { nums[k] = nums[i] }