Selection sort¶
Content summary
This lesson covers:
- The sorting problem
- The selection sort algorithm
Overview¶
Problem statement¶
Given a class gradebook, how can we quickly find the top 10 students with the highest scores?
One simple and effective solution is to sort the entire list in descending order. After that, the first 10 students in the sorted list are exactly the ones with the highest grades.
The sorting problem¶
Sorting data is a fundamental and important task in data processing. It makes searching, retrieving, and analyzing data much easier and faster.
Sorting means rearranging data into a meaningful order.
In topic F, we only study the simple case of sorting a one-dimensional array in non-decreasing order (1).
-
Non-decreasing allows equal values: each next element is greater than or equal to the previous one.
Meanwhile, strictly increasing does not allow duplicates — each next element must be strictly greater than the previous one.
Formal problem statement:
| Input | Output |
|---|---|
A one-dimensional array A with n integers |
The same array A, now sorted in non-decreasing order |
Sorting algorithms¶
There are many different sorting algorithms. Most of them work by comparing elements with each other to decide which one should come first.
This lesson focuses on the selection sort algorithm.
Benefits and real-world applications
Sorting data makes it easier to read, faster to search, and simpler to process.
In practice, sorting algorithms are used in a wide variety of problems, including:
- Management systems — sorting IDs, names, dates, locations, etc.
- Graph algorithms such as Prim, Kruskal, and Dijkstra — sorting edges by weight.
- Statistics — finding the median, quartiles, percentiles.
- Removing duplicates, merging datasets, divide-and-conquer strategies, range searches.
- Scheduling and simulation — ordering events by time in games, job schedulers, operating systems, or network packet processing.
Today, almost every programming language and software library already provides built-in, highly efficient sorting functions.
Even so, learning how sorting algorithms work remains very valuable — it helps you develop logical thinking, understand performance differences, and write better code.
Selection sort algorithm¶
Idea¶
Imagine you need to pick the best possible starting lineup from a group of players.
You do it like this: First, pick the best player. Then, from the remaining players, pick the second-best. Then the third-best, and so on.
Now apply this to an array.
If we treat "smallest" as "best", the strategy becomes: Find the smallest element and put it in the first position. Then find the next smallest and put it in the second position. Keep going until the array is fully sorted.
Here’s how it works step by step:
For each position i from the beginning of the array up to the second-to-last element:
- Scan the unsorted portion (from index
ito the end) to find the smallest element and remember its indexmin. - Swap
A[i]withA[min].
This process gradually builds the sorted part at the beginning of the array.
The main idea is illustrated below:
Example illustration¶
Flowchart¶
Visualization¶
Writing the program¶
1. Import the numpy library.
2. Write the function selection_sort() to implement the selection sort algorithm.
3. Write the main program.
In the main program, we will not ask the user to input the entire array. Instead, we create a fixed array in the code and then call the selection_sort() function to sort the array Array.
4. Run the program above. The output will be:
Source code¶
The complete code is available at:
Summary mindmap¶
Some English words¶
| Vietnamese | Tiếng Anh |
|---|---|
| hoán vị (hai phần tử) | swap |
| sắp xếp chọn | selection sort |
| so sánh | compare |