Sorting is a simple and intuitive selection sorting algorithm, no matter what the data into the time complexity is O (n²) of. So use it when the data size as small as possible. The only advantage is probably not take up extra memory space bar. 1 . Algorithm steps First, find the smallest sequence of unsorted (large) element, the starting position is stored in the sorted sequence. From the remaining unsorted then continue to look for the minimum element (large) element, and then into the end of the sorted sequence. The second step is repeated until all the elements are sorted completed.
Code JavaScript code implementation function selectionSort(arr) { var len = arr.length; var minIndex, temp; for (var i = 0; i <len - 1; i ++ ) { minIndex = I; for (var = I + J. 1; J <len; J ++ ) { IF (ARR [J] <ARR [minIndex]) {// find the smallest number minIndex = J; // index of the minimum number of storage } } temp = arr[i]; arr [i] = arr [minIndex]; arr [minIndex] = temp; } return arr; }
Python code implements DEF SelectionSort (ARR): for I in Range (len (ARR) -. 1 ): # record the minimum number of index minIndex = I for J in Range (I +. 1 , len (ARR)): IF ARR [J ] < ARR [minIndex]: minIndex = J # i when not a minimum number, and i is the minimum number of exchanged IF i =! minIndex: arr [i], arr [minIndex] = arr [minIndex], arr [i] return arr
Go code implementation func selectionSort(arr []int) []int { length := len(arr) for i := 0; i < length-1; i++ { min := i for j := i + 1; j < length; j++ { if arr[min] > arr[j] { min = J } } arr [i], arr [min] = arr [min], arr [i] } return arr }
Java code implementation public class SelectionSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); // go through a total of N-1 wheel comparison for (int I = 0; I <arr.length -. 1; I ++ ) { you're a = s; // round number to be compared N- I for (int I = J +. 1; J ++; J <arr.length ) { IF (ARR [J] < ARR [min]) { // minimum recording can find the current index value of the element min = j; } } // to find the minimum value of i and the location of the exchange IF (i! = Min) { int tmp = arr[i]; arr[i] = arr[min]; arr [min] = tmp; } } return arr; } }
PHP code implementation function selectionSort($arr) { $len = count($arr); for ($i = 0; $i < $len - 1; $i++) { $minIndex = $i; for ($j = $i + 1; $j < $len; $j++) { if ($arr[$j] < $arr[$minIndex]) { $ minIndex = $ j; } } $temp = $arr[$i]; $ arr [$ i] = $ arr [$ minIndex]; $ arr [$ minIndex] = $ temp; } return $arr; }
C language the swap void (int * A, * B int) // swap the two variables { int temp = *a; *a = *b; *b = temp; } void selection_sort(int arr[], int len) { int i,j; for (i = 0 ; i < len - 1 ; i++) { min int = I; for (I = J +. 1; J <len; J ++) // visited element unordered IF (ARR [J] <ARR [min]) // find the minimum value of the current min = J; // record the minimum the swap ( & ARR [min], & ARR [I]); // do the exchange } }
++ C Template <typename T> // integer or floating point can be used, to use the object ( class must be set greater than) (> ) the operator functions void selection_sort(std::vector<T>& arr) { for (int i = 0; i < arr.size() - 1; i++) { int min = i; for (int j = i + 1; j < arr.size(); j++) if (arr[j] < arr[min]) min = j; std::swap(arr[i], arr[min]); } }
C # static void selection_sort <T> (T [] ARR) WHERE T: the System.IComparable <T> {// integer or floating the use of either int i, j, min, len = arr.Length; T temp; for (i = 0; i < len - 1; i++) { min = i; for (j = i + 1; j < len; j++) if (arr[min].CompareTo(arr[j]) > 0) min = j; temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } }
SWIFT Import Foundation /// selection sort /// /// - the Parameter List: the need to sort an array func selectionSort(_ list: inout [Int]) -> Void { for j in 0..<list.count - 1 { var minIndex = j for i in j..<list.count { if list[minIndex] > list[i] { minIndex = i } } list.swapAt(j, minIndex) } }