Selection Sort
June 24, 2025
04:33 AM
No headings found
Loading content...
Related Posts
Theory Data Structure And Algorithms
No headings found
Related Posts
Theory Data Structure And Algorithms
Approach
Selection Sort là một thuật toán sắp xếp đơn giản, hoạt động theo nguyên tắc sau:
Ưu điểm:
Nhược điểm:
Solution
1export default function selectionSort(arr: Array<number>): Array<number> {
2 for (let i = 0; i < arr.length; i++) {
3 let min_idx = i;
4 for (let j = i + 1; j < arr.length; j++) {
5 if (arr[j] < arr[min_idx]) min_idx = j;
6 }
7 [arr[i], arr[min_idx]] = [arr[min_idx], arr[i]];
8 }
9 return arr;
10}