Simple selection sorting is a sort algorithm that is often used.
Principle:
1. Simple selection Sort In a nutshell: Each time you select an unordered sequence, the smallest one is placed at the end of an ordered sequence.
2. In the simple selection sort, we use the initialized number int a[6]={2,5,6,3,1,4}
3. The algorithm basically performs step 1: Finds the bottom-most number in the initial unordered array, placing it in the head of the array. Swap the minimum and array head elements (this is where bubbles differ).
1#include <stdio.h>2#include <stdlib.h>3 4 /*5 * Simple Selection sort in a nutshell: Each time you select an unordered sequence, the smallest one is placed at the end of an ordered sequence. 6 * Second, in the simple selection sort, we use the initialized number int a[6]={2,5,6,3,1,4}7 * Three, the algorithm basic implementation Step 1: Find the initial unordered array of the bottom number, put it in the head of the array. Swap the minimum and array head elements (this is where bubbles differ). 8 */9 Ten //Replace One voidSwapintA[],intIintj) { A inttemp =A[i]; -A[i] =A[j]; -A[J] =temp; the } - - //Quick Sort - voidSelect_sort (intA[],intl) + { - //L = 6 + inti,temp,j,k; A /* at * Derivation Process 2,5,6,3,1,4 - * i = 0 temp = 2, k = 0 Search to minimum 1, k = 4-> 1,5,6,3,2,4 - * i = 1 temp = 5, k = 1 Search to minimum 2, k = 4, 1,2,6,3,5,4 - * i = 2 temp = 6, K = 2 Search to minimum 3 k = 3-1,2,3,6,5,4 - * i = 3 temp = 6, K = 3 Search to minimum 4 K = 5-1,2,3,4,5,6 - * i = 4 temp = 5, k = 4 search less than k = 4 K and I do not do any action in */ - to //no loops for the last time + for(i=0; i<l-1; i++){ - //gets the value of the header element thetemp =A[i]; *K =i; $ Panax Notoginseng //printf ("%d\n", temp); - //Search starting from the i+1 element, if found smaller than the first element, replace the for(j = i+1; j<l;j++){ + A if(A[j] <temp) { the //record the minimum value first +temp =A[j]; -K = J;//Subscript of the minimum value $ } $ } - - //Replace the if(I! =k) - swap (a,i,k);Wuyi } the - } Wu - voidMain () About { $ intA[] = {2,5,6,3,1,4}; - intI,l =sizeof(a)/sizeof(int); - - //Select Sort A Select_sort (a,l); + theprintf"after selecting sort: \ n"); - for(i =0; I < L; ++i) { $printf"%d\n", A[i]); the } the the}
C Language Selection sorting