Select a sort of C + + implementation
First, source code: SelectSort.cpp
1 /*2 The basic idea of choosing a sort (small to large) is to first select the smallest number and put it in the first position;3 then, the second small number is selected and placed in the second position;4 and so on, until all the numbers are sorted from small to large. 5 in implementation, we usually first determine the position of the small number of I, and then, it is exchanged with the number of the first. 6 */7#include <iostream>8 using namespacestd;9 /*functions that define the output of a one-dimensional array*/Ten voidPrintintArray[],intN) One { A for(inti =0; I < n; i++) - { -cout << Array[i] <<" "; the } -cout <<Endl; - } - + intSelectsort (intArray[],intN) - { + //define variables, record interchange times A intCount =0; at //assuming the minimum value is in the same position, the default is 0, which is the first element - intMin_index =0; - //defining intermediate variables, storing temporary data - inttemp; - //iterating through an array (sorting) -cout <<"begin sorting an array ..."<<Endl; in for(inti =0; I < n-1; i++) - { to //assuming that the current element is a small element of I, save the current position +Min_index =i; - for(intj = i +1; J < N; J + +) the { *cout <<"Section"<< (i +1) <<"Trip No."<< (j +1) <<"Second Order"<<Endl; $ //determines whether the current element is less than the hypothetical I small elementPanax Notoginseng if(array[min_index]>Array[j]) - { the //Reset the small element position of I +Min_index =J; A } the } + //determines whether the element at the current position is equal to the hypothetical I small element, and if not equal, swaps the two elements - if(Min_index! =i) $ { $temp =Array[min_index]; -Array[min_index] =Array[i]; -Array[i] =temp; thecout << Array[min_index] <<"and the"<< Array[i] <<"swapped out"<<Endl; - //output The order of the array at this timeWuyicout <<"the order of the arrays at this time is:"; thePrint (Array,Ten); - //Record Count plus 1 per Exchange Wucount++; - } About } $cout <<"array sort ended ..."<<Endl; - returncount; - } - A intMain () + { the //define a one-dimensional array to sort - intArray[] = {1,3,4,5,2,6,Ten,9,8,7 }; $ //Output Original Array thecout <<"the original array is:"<<Endl; thePrint (Array,Ten); the //sorting an array the intCount = Selectsort (Array,Ten); - //output sorted Array incout <<"the sorted array is:"<<Endl; thePrint (Array,Ten); thecout <<"co-Exchange"<< Count <<"Times"<<Endl; About return 0; the}
Second, the Operation effect
Select a sort of C + + implementation