?? In the beginning of the learning array, not very good use of simple sorting algorithm, when the deep learning for a period of time, want $ clear, today here is a simple to share the use of the selection of sorting algorithm and related examples.
1. What is the selection sort?
?? From the literal meaning can understand, is to have the choice to carry on the order, then what is the choice? According to my understanding, is according to a certain rule (for example, from large to small or small to large) to sort, as if we go to see the beauty, it must be the first to find the best temperament, down, of course, according to temperament to choose, this is a condition of judgment, is one of our standards , and the sort of choices we learn is the same.
Professional Explanation:
?? Select sort is to achieve an orderly arrangement of the entire data table by selecting the minimum (large) of the keywords from the records to be sorted during each trip and then placing them in the first or last end of the data table in turn.
Ideas:
?? The first order selects the smallest record in all the N records to be sorted, exchanges it with the first record in the data table, and the smallest record of the keyword is at the front of the data table;
?? The second time in the remaining N-1 records, the smallest record of the key is selected, and the second record in the data table to exchange the location, so that the key is small record in the second position of the data table;
?? This is repeated all the time, resulting in an ascending order of the data tables. A total of n-1 is ordered.
Idea Picture Analysis:
1. Process trend:
2. Data examples:
2. Select Sort to implement Demo
-
//Selection sorting algorithm function implementationvoidSelect_sort (int*arr,intLen) { inti =0; intj =0; intindex =0;//record the location of the smallest record intMin_value =0;//Record Minimum value for(i =0; i < Len; i++) {Min_value=Arr[i]; //get the minimum value and its position for(index = i, j = i +1; J < Len; J + +) { if(Min_value >Arr[j]) {Min_value=Arr[j]; Index=J; } } //Exchange The position of the value of the position I and the minimumarr[index]=Arr[i]; Arr[i]=Min_value; }}
The rest of the code
-
//Print Output functionvoidPrint_arr (int*arr,intLen) { inti =0; for(i =0; i < Len; i++) printf ("%d\t", Arr[i]); printf ("\ n");}intMainvoid){ intarr[]={ A,1, the,3, About, the, at, $, -}; //gets the array length len intLen =sizeof(arr)/sizeof(arr[0]); printf ("init\n"); Print_arr (arr, Len); //Call sort functionSelect_sort (arr, Len); printf ("Sort later\n"); Print_arr (arr, Len); Return0;}
Operation Result:
Sorting the array by selecting sort