Selection Sort Selection Sort is a sorting method similar to Insertion Sort. It is also only suitable for sorting small sets. Its core idea is to logically divide the sequence into two parts, sorted and unsorted, constantly find the elements that best match the sorting rules in the unsorted part, add them to the sorted part, until all elements in the sequence have been added to the sorted part, and at this time, the entire sequence is sorted.
Bubble sorting is to exchange it continuously to achieve sorting in pairs, so it is more cumbersome.
When choosing sorting, you first select the number to be exchanged before you exchange it. This will save a lot of unnecessary steps.
Swift version implementation example:
func selectSort(var arr: [Int]) ->[Int] { var min = 0 // Only n-1 trips are needed for var i = 0; i < - 1; ++i { min = i // Find the end from the n+1st journey for var j = i + 1; j < ; ++j { // Find a smaller location than the min location, and update the minimum location found in this trip if arr[j] < arr[min] { min = j } } // If min and i are not equal, it means that there is a smaller position than i, so it needs to be exchanged if min != i { let temp = arr[i] arr[i] = arr[min] arr[min] = temp } } return arr }