Improve the consistency between code and comments in Kotlin (#1268)

* style(kotlin): Make code and comments consistent.

* style(kotlin): convert comment location.

* style(c): Add missing comment.

* style(kotlin): Remove redundant semicolon, parenthesis and brace

* style(kotlin): Put constants inside the function.

* style(kotlin): fix unnecessary indentation.

* style(swift): Add missing comment.

* style(kotlin): Add missing comment.

* style(kotlin): Remove redundant comment.

* style(kotlin): Add missing comment.

* Update linked_list.kt

* style(csharp,js,ts): Add missing comment.

* style(kotlin): Remove empty lines.

* Update list.cs

* Update list.js

* Update list.ts

* roll back to commit 1

* style(cs,js,ts): Add missing comment in docfile.

* style(kotlin): Use normal element swapping instead of scope functions.
This commit is contained in:
curtishd
2024-04-13 20:09:39 +08:00
committed by GitHub
parent b0d6fa0f38
commit 4d9bbe72e1
25 changed files with 74 additions and 61 deletions

View File

@@ -14,7 +14,9 @@ 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] }
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
}
}
}
@@ -29,7 +31,9 @@ fun bubbleSortWithFlag(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] = nums[j + 1] }
val temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
flag = true // 记录交换元素
}
}

View File

@@ -22,7 +22,9 @@ fun siftDown(nums: IntArray, n: Int, li: Int) {
if (ma == i)
break
// 交换两节点
nums[i] = nums[ma].also { nums[ma] = nums[i] }
val temp = nums[i]
nums[i] = nums[ma]
nums[ma] = temp
// 循环向下堆化
i = ma
}
@@ -37,7 +39,9 @@ fun heapSort(nums: IntArray) {
// 从堆中提取最大元素,循环 n-1 轮
for (i in nums.size - 1 downTo 1) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
nums[0] = nums[i].also { nums[i] = nums[0] }
val temp = nums[0]
nums[0] = nums[i]
nums[i] = temp
// 以根节点为起点,从顶至底进行堆化
siftDown(nums, i, 0)
}

View File

@@ -8,7 +8,9 @@ package chapter_sorting
/* 元素交换 */
fun swap(nums: IntArray, i: Int, j: Int) {
nums[i] = nums[j].also { nums[j] = nums[i] }
val temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
}
/* 哨兵划分 */

View File

@@ -18,7 +18,9 @@ fun selectionSort(nums: IntArray) {
k = j // 记录最小元素的索引
}
// 将该最小元素与未排序区间的首个元素交换
nums[i] = nums[k].also { nums[k] = nums[i] }
val temp = nums[i]
nums[i] = nums[k]
nums[k] = temp
}
}