mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 19:54:20 +00:00
build
This commit is contained in:
@@ -306,7 +306,22 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="selection_sort.rb"
|
||||
[class]{}-[func]{selection_sort}
|
||||
### 选择排序 ###
|
||||
def selection_sort(nums)
|
||||
n = nums.length
|
||||
# 外循环:未排序区间为 [i, n-1]
|
||||
for i in 0...(n - 1)
|
||||
# 内循环:找到未排序区间内的最小元素
|
||||
k = i
|
||||
for j in (i + 1)...n
|
||||
if nums[j] < nums[k]
|
||||
k = j # 记录最小元素的索引
|
||||
end
|
||||
end
|
||||
# 将该最小元素与未排序区间的首个元素交换
|
||||
nums[i], nums[k] = nums[k], nums[i]
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user