Ten classic choice of sorting algorithm to sort

Select [Sort] (Select Sort)

a) Principle

Selection sort works to find the remaining array elements in the minimum (or maximum) element, and then put the beginning of the array is sorted. And so on, until all the elements of the sort is complete.

b)     demonstrate dynamic FIG.

 

c)     algorithm steps

l Find all the elements, find the smallest element in the array, and remember its index (coordinates);

l the minimum switching element to the first element, so that the smallest element in the forefront;

l find the remaining elements, to find the minimum and the second switching element.

l and so on.

c) code implementation

 1 #include <iostream>
 2 
 3 using namespace std;
 4 void SelectSort(int [],int);
 5 void PrintStr(int [],int);
 6 
 7 int main()
 8 {
 9     int str[] = {2,5,6,1,53,26,64,18,98,11};
10     int= strlen the sizeof (STR) / the sizeof (STR [ 0 ]);
 . 11      COUT << " : Array sequence before sorted " ;
 12 is      PrintStr (STR, strlen);
 13 is      ; SelectSort (STR, strlen)
 14      COUT << " sorted array sequence: " ;
 15      PrintStr (STR, strlen);
 16      return  0 ;
 . 17  }
 18 is  void SelectSort ( int STR [], int strlen)
 . 19  {
 20 is      int minIndex, TEMP;
 21 is      for( Int i = 0 ; i <strlen; i ++ )
 22      {
 23          minIndex = i;
24          for ( int j = i + 1 ; j <strlen; s ++ )
 25          {
 26              if (str [s] < str [minIndex])
 27              {
 28                  minIndex = j; // 找到最小的数的索引
29              }
 30          }
 31          temp = str [minIndex];
32          str [minIndex] = str [i];
33         str[i] = temp;
34     }
35 }
36 void PrintStr(int str[],int strlen)
37 {
38     for (int i = 0;i < strlen;i++)
39         cout << str[i] << ',';
40     cout << endl;
41 }

 

Reference blog: https: //www.cnblogs.com/onepixel/articles/7674659.html, thanks again!

Guess you like

Origin www.cnblogs.com/xdak/p/10994990.html