Select Sort Description
Select sort (Selection sort) is a simple and intuitive sorting algorithm.
The basic idea is to first find the smallest (or largest) element in an unordered sequence, and then store it in the beginning of the sequence, and then continue looking for the smallest (or largest) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are sorted.
Summary: Two for loop = one for like i=0-(n-1) + A For loop looking for the coordinates of the smaller array than I save with min + an exchange process
/** set a reference storage coordinate, and the difference between bubbles is not the need for frequent mobile switching, it just need to find the coordinates to save it. */#include"stdio.h"/** Select Sort * * Parameter description: * A--array to be sorted * N--The length of the array*/voidSelect_sort (intA[],intN) { intI//the end position of the ordered area intJ//starting position of the unordered area intMin//the smallest element position in an unordered region for(i=0; i<n;i++) { intflag=1; Min=i;//set a reference storage coordinate, and the difference between bubbles is that there is no need to move frequently for(j=i+1; j<n;j++)//It starts bubbling, but it doesn't have to be bubbling, so how do you find the smallest? { if(a[j]<a[min]) min=j;//to find the smallest a[j of the disordered zone in such a loop] to minflag=0; } if(!flag)//If the above appears smaller than I exchange, there is no need to exchange { intTemp=a[min];//Exchange A[i] and A[min]A[min]=a[i];//a[i]=temp; } }}voidMainvoid){ inta[]={5,9, -,2,7, -, $,3, -, A,Ten}; intLength=sizeof(a)/(sizeof(a[0])); printf ("before sorting:"); for(intI=0; i<length;i++) printf ("%d", A[i]); printf ("\ n"); Select_sort (a,length); printf ("after selecting sort:"); for(intI=0; i<length;i++) printf ("%d", A[i]); printf ("\ n");}
Data structure _ Select Sort