Simply select the C ++ Implementation of sorting and select sorting implementation
Simple selection and sorting use the simplest selection method, that is, the minimum (or maximum) keyword is selected in the residual sequence, and the position is exchanged with the first keyword of the residual sequence, until the entire sequence is ordered.
Directly run the Code:
# Include <iostream> # include <string> using namespace std; template <typename T> void selectionSort (T arr [], int n) {for (int I = 0; I <n; I ++) {// find the minimum int minIndex = I in the [I, n) interval; // mark the position of the minimum value for (int j = I + 1; j <n; j ++) if (arr [j] <arr [minIndex]) minIndex = j; swap (arr [I], arr [minIndex]) ;}} int main () {int array [10] =, 2, 1}; selectionSort (array, 10); for (int I = 0; I <10; I ++) cout <array [I] <""; cout <endl; float a [3] = {3.3f, 2.2f, 1.1f}; selectionSort (a, 3); for (int j = 0; j <3; j ++) cout <a [j] <"; cout <endl; string B [4] = {" D "," C "," B ", "A"}; selectionSort (B, 4); for (int k = 0; k <4; k ++) cout <B [k] <""; cout <endl; return 0 ;}