The content is introduced
The idea of selection sorting
The smallest element is first selected from the data to be sorted and stored at the beginning of the sequence. The smallest element is then found from the remaining unsorted elements and placed at the end of the sorted sequence. And so on until the total number of data elements to be sorted is zero.
Select sort animation presentation
Selective sorting analysis
In general, there is no special requirement for sorting algorithms to sort in ascending order, small first, big last. The array consists of {6, 5, 4, 1, 3, 2} unordered elements.
How selection sort works: Find the minimum value of this round and place it on the far left.
Before the first round of exchange:
After the first round of exchange:
Before the second round of exchange:
After the second round of exchange:
The middle third round and the fourth round are similar.
Before the fifth round of exchange:
After the fifth round of exchange:
Select sort code to write
We analyzed the principle of selection sorting and found that 6 elements need to be compared for 5 rounds, which need to be controlled through a cycle, and the number of rounds is the number of elements -1. Each round needs to find the minimum value among the remaining elements, and a loop is also used. So you need to use nested loops.
The code is as follows:
public class SelectionSortTest {
public static void main(String[] args) {
int[] arr = new int[] {6.5.4.1.3.2};
selectionSort(arr);
}
// For the first time, select the smallest element from the data elements to be sorted and store it at the beginning of the sequence.
// Find the smallest element from the remaining unsorted elements and place it at the end of the sorted sequence.
// And so on until the total number of data elements to be sorted is zero. Selection sort is an unstable sort method.
// Select sort must traverse all subsequent elements in each round to determine a minimum value.
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) { // The outer loop controls the number of rounds to compare
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) { // Loop inside to find the minimum value
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
System.out.println("The first" + (i + 1) + "After round comparison:" + Arrays.toString(arr));
}
}
}
Copy the code
The running effect is as follows:
The first1After round comparison: [1.5.4.6.3.2]
The first2After round comparison: [1.2.4.6.3.5]
The first3After round comparison: [1.2.3.6.4.5]
The first4After round comparison: [1.2.3.4.6.5]
The first5After round comparison: [1.2.3.4.5.6]
Copy the code
conclusion
- The principle of selective sorting is to first select the smallest element from the data to be sorted, store it at the beginning of the sequence, then find the smallest element from the remaining unsorted elements, and place it at the end of the sorted sequence. And so on until the total number of data elements to be sorted is zero.
- Selection sorting uses two loops when writing code: the outer loop controls the number of rounds to compare, and the inner loop finds the minimum
———- End ———-
Original articles and animation production is really not easy, your thumbs up is the biggest support!
For more articles, please pay attention to wechat public number: Cousin Animation learning programming