Update the comments of bubble sort

and insertion sort
This commit is contained in:
krahets
2023-05-22 23:05:37 +08:00
parent 5b406666d8
commit f6d290d903
36 changed files with 119 additions and 119 deletions

View File

@@ -6,9 +6,9 @@
/* */
func bubbleSort(nums: inout [Int]) {
// n-1, n-2, ..., 1
// [0, i]
for i in stride(from: nums.count - 1, to: 0, by: -1) {
//
// [0, i]
for j in stride(from: 0, to: i, by: 1) {
if nums[j] > nums[j + 1] {
// nums[j] nums[j + 1]
@@ -22,7 +22,7 @@ func bubbleSort(nums: inout [Int]) {
/* */
func bubbleSortWithFlag(nums: inout [Int]) {
// n-1, n-2, ..., 1
// [0, i]
for i in stride(from: nums.count - 1, to: 0, by: -1) {
var flag = false //
for j in stride(from: 0, to: i, by: 1) {

View File

@@ -6,16 +6,16 @@
/* */
func insertionSort(nums: inout [Int]) {
// base = nums[1], nums[2], ..., nums[n-1]
// 1, 2, ..., n
for i in stride(from: 1, to: nums.count, by: 1) {
let base = nums[i]
var j = i - 1
// base
// base
while j >= 0, nums[j] > base {
nums[j + 1] = nums[j] // 1. nums[j]
nums[j + 1] = nums[j] // nums[j]
j -= 1
}
nums[j + 1] = base // 2. base
nums[j + 1] = base // base
}
}