Share selection sort c++,

ducangene

New member
#C ++, #Sorting, #algorithms, #DatScatures, #Programming ## Selection Sắp xếp trong C ++

Sắp xếp lựa chọn là một thuật toán sắp xếp hoạt động bằng cách liên tục tìm phần tử nhỏ nhất trong danh sách chưa được phân loại và hoán đổi nó với phần tử đầu tiên của danh sách.Quá trình này được lặp lại cho đến khi danh sách được sắp xếp.

Loại lựa chọn là một thuật toán đơn giản, nhưng nó không hiệu quả lắm.Độ phức tạp của thời gian của nó là O (n^2), trong đó n là số lượng các phần tử trong danh sách.Điều này có nghĩa là thời gian chạy của lựa chọn sắp xếp phát triển bậc hai với kích thước của danh sách.

Tuy nhiên, loại lựa chọn vẫn là một thuật toán hữu ích để sắp xếp các danh sách nhỏ.Nó cũng là một thuật toán tốt để hiểu, vì nó minh họa các khái niệm cơ bản về phân loại.

### Thuật toán

Sau đây là mã giả để sắp xếp lựa chọn:

`` `
Chức năng lựa chọnSort (mảng) {
for (let i = 0; i <mảng.length - 1; i ++) {
// Tìm phần tử nhỏ nhất trong phần chưa được phân loại của mảng.
để nhỏ nhất = i;
for (let j = i+1; j <mảng.length; j ++) {
if (mảng [j] <mảng [nhỏ nhất]) {
nhỏ nhất = j;
}
}

// Trao đổi phần tử nhỏ nhất với phần tử đầu tiên của phần chưa được phân loại của mảng.
const temp = mảng ;
mảng = mảng [nhỏ nhất];
mảng [nhỏ nhất] = temp;
}
}
`` `

### Độ phức tạp thời gian

Độ phức tạp về thời gian của loại lựa chọn là O (n^2), trong đó n là số lượng các phần tử trong danh sách.Điều này là do thuật toán phải quét toàn bộ danh sách n lần và mỗi lần nó phải tìm phần tử nhỏ nhất trong phần chưa được phân loại của danh sách.

### Độ phức tạp không gian

Độ phức tạp không gian của loại lựa chọn là O (1).Điều này là do thuật toán không yêu cầu bất kỳ không gian lưu trữ bổ sung nào ngoài danh sách ban đầu.

### Các ứng dụng

Sắp xếp lựa chọn là một thuật toán đơn giản và hiệu quả để sắp xếp các danh sách nhỏ.Nó cũng là một thuật toán tốt để hiểu, vì nó minh họa các khái niệm cơ bản về phân loại.

Sắp xếp lựa chọn thường được sử dụng để sắp xếp dữ liệu theo mảng.Nó cũng có thể được sử dụng để sắp xếp dữ liệu trong các danh sách được liên kết, nhưng nó không hiệu quả trong trường hợp này.

### Người giới thiệu

* [Wikipedia: Sắp xếp lựa chọn] (Selection sort - Wikipedia)
=======================================
#C++, #Sorting, #algorithms, #datastructures, #Programming ## Selection Sort in C++

Selection sort is a sorting algorithm that works by repeatedly finding the smallest element in an unsorted list and swapping it with the first element of the list. This process is repeated until the list is sorted.

Selection sort is a simple algorithm, but it is not very efficient. Its time complexity is O(n^2), where n is the number of elements in the list. This means that the running time of selection sort grows quadratically with the size of the list.

However, selection sort is still a useful algorithm for sorting small lists. It is also a good algorithm to understand, as it illustrates the basic concepts of sorting.

### Algorithm

The following is the pseudocode for selection sort:

```
function selectionSort(array) {
for (let i = 0; i < array.length - 1; i++) {
// Find the smallest element in the unsorted part of the array.
let smallestIndex = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[smallestIndex]) {
smallestIndex = j;
}
}

// Swap the smallest element with the first element of the unsorted part of the array.
const temp = array;
array = array[smallestIndex];
array[smallestIndex] = temp;
}
}
```

### Time Complexity

The time complexity of selection sort is O(n^2), where n is the number of elements in the list. This is because the algorithm must scan the entire list n times, and each time it must find the smallest element in the unsorted part of the list.

### Space Complexity

The space complexity of selection sort is O(1). This is because the algorithm does not require any additional storage space beyond the original list.

### Applications

Selection sort is a simple and efficient algorithm for sorting small lists. It is also a good algorithm to understand, as it illustrates the basic concepts of sorting.

Selection sort is often used to sort data in arrays. It can also be used to sort data in linked lists, but it is not as efficient in this case.

### References

* [Wikipedia: Selection sort](https://en.wikipedia.org/wiki/Selection_sort)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top