Add the section of selection sort. (#513)
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* File: selection_sort.cpp
|
||||||
|
* Created Time: 2023-05-23
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../utils/common.hpp"
|
||||||
|
|
||||||
|
/* 选择排序 */
|
||||||
|
void selectionSort(vector<int> &nums) {
|
||||||
|
int n = nums.size();
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间 [i, n-1] 中的最小元素
|
||||||
|
int k = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (nums[j] < nums[k]) {
|
||||||
|
k = j; // 更新最小元素
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将该最小元素与未排序区间的首个元素交换
|
||||||
|
swap(nums[i], nums[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
int main() {
|
||||||
|
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||||
|
selectionSort(nums);
|
||||||
|
|
||||||
|
cout << "选择排序完成后 nums = ";
|
||||||
|
printVector(nums);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* File: selection_sort.java
|
||||||
|
* Created Time: 2023-05-23
|
||||||
|
* Author: Krahets (krahets@163.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package chapter_sorting;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class selection_sort {
|
||||||
|
/* 选择排序 */
|
||||||
|
public static void selectionSort(int[] nums) {
|
||||||
|
int n = nums.length;
|
||||||
|
// 外循环:未排序区间为 [i, n-1]
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
// 内循环:找到未排序区间 [i, n-1] 中的最小元素
|
||||||
|
int k = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (nums[j] < nums[k]) {
|
||||||
|
k = j; // 更新最小元素
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将该最小元素与未排序区间的首个元素交换
|
||||||
|
int temp = nums[i];
|
||||||
|
nums[i] = nums[k];
|
||||||
|
nums[k] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||||
|
selectionSort(nums);
|
||||||
|
System.out.println("选择排序完成后 nums = " + Arrays.toString(nums));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
"""
|
||||||
|
File: selection_sort.py
|
||||||
|
Created Time: 2023-05-22
|
||||||
|
Author: Krahets (krahets@163.com)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def selection_sort(nums: list[int]):
|
||||||
|
"""选择排序"""
|
||||||
|
n = len(nums)
|
||||||
|
# 外循环:未排序区间为 [i, n-1]
|
||||||
|
for i in range(n - 1):
|
||||||
|
# 内循环:找到未排序区间 [i, n-1] 中的最小元素
|
||||||
|
k = i
|
||||||
|
for j in range(i + 1, n):
|
||||||
|
if nums[j] < nums[k]:
|
||||||
|
k = j # 更新最小元素
|
||||||
|
# 将该最小元素与未排序区间的首个元素交换
|
||||||
|
nums[i], nums[k] = nums[k], nums[i]
|
||||||
|
|
||||||
|
|
||||||
|
"""Driver Code"""
|
||||||
|
if __name__ == "__main__":
|
||||||
|
nums = [4, 1, 3, 1, 5, 2]
|
||||||
|
selection_sort(nums)
|
||||||
|
print("选择排序完成后 nums =", nums)
|
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 53 KiB |