Selection sort algorithm (Selection Sort)
Selection Sort is a simple algorithm. This sorting algorithm is an algorithm based on in-place comparison, in which the list is divided into two parts, sorted (list) on the left and unsorted (unsorted list) in the right. Initially, the sorted part is blank and the unordered part is the original list.
What is Selection Sort?
Selection Sort is a simple algorithm. This sorting algorithm is an algorithm based on in-place comparison, in which the list is divided into two parts, sorted (list) on the left and unsorted (unsorted list) in the right. Initially, the sorted part is blank and the unordered part is the original list.
The smallest element selected from the array has not been sorted and is swapped with the most left part and that element becomes the element of the sorted array. This process continues until all of the unmatched array elements are moved to the sorted array.
This algorithm is not suitable for large data sets where the worst case complexity and the average case for O (n2) with n are the number of elements.
You learn the in-place concept in chapter: Some basic concepts about sorting algorithms.
Selection sort works (Selection Sort) works
Below are illustrations of how the algorithm works. Suppose we have an array as follows:
From the first position in the list has been sorted, the entire list is approved continuously. The first position has a value of 14, we find the entire list and find that 10 is the smallest value.
Therefore, we replace 14 with 10. After a loop, value 10 replaces the value 14 at the first position in the sorted list. We swap these two values.
In second place, value 33, we continue to scan the rest of the list in order of each element.
We see that 14 is the second smallest value in the list and it should appear in the second position. We swap these two values.
After two loops, the two smallest values have been placed at the beginning of the sorted list.
The same process will apply to the rest of the list. The figures below illustrate these processes.
Next we will monitor some other aspects of the selection algorithm.
Selection sort algorithm (Selection Sort)
Bước 1 : Thiết lập MIN về vị trí 0 Bước 2 : Tìm kiếm phần tử nhỏ nhất trong danh sách Bước 3 : Tráo đổi với giá trị tại vị trí MIN Bước 4 : Tăng MIN để trỏ tới phần tử tiếp theo Bước 5 : Lặp lại cho tới khi toàn bộ danh sách đã được sắp xếp
Sample algorithm for sort selection
B ắ t đầ u gi ả i thu ậ t s ắ p x ế p ch ọ n ( Selection Sort ) list : m ả ng c á c ph ầ n t ử n : k í ch c ỡ m ả ng for i = 1 t ớ i n - 1 /* thiết lập phần tử hiện tại là min*/ min = i /* kiểm tra phần tử có là nhỏ nhất không */ for j = i + 1 t ớ i n if list [ j ] < list [ min ] th ì min = j ; k ế t th ú c if k ế t th ú c for /* tráo đổi phần tử nhỏ nhất với phần tử hiện tại*/ if indexMin != i then tr á o đổ i list [ min ] v à list [ i ] k ế t th ú c if k ế t th ú c for K ế t th ú c gi ả i thu ậ t
According to Tutorialspoint
Previous lesson: Insert algorithm (Insertion Sort)
Next lesson: Mixing algorithm (Merge Sort)
You should read it
- Shell Sort in data structure and algorithm
- Quick Sort (Quick Sort)
- Insert algorithm (Insertion Sort)
- Alignment algorithm (Merge Sort)
- Binary Search algorithm (Binary Search)
- What is algorithm?
- Bubble Sort (Bubble Sort)
- How does YouTube algorithm work?
- Panda 4.0 algorithm was updated by Google on May 20, 2014
- Greedy Algorithm (Greedy Algorithm)
- Facebook is about to change the algorithm, articles that use 'interactive traps' will be downgraded
- Toshiba's algorithm helps normal computers calculate faster than supercomputers
Maybe you are interested
How to connect PS5 controller to Windows PC, Mac, Steam Tabletop Simulator: Must-try co-op games How to connect a PlayStation 5 controller on Steam 10 important sayings should tell the opponent every day Mysterious cosmic signals of aliens challenge the scientific world SpaceX successfully launched the most 'heavy' rocket to geostationary orbit