[Analog] Selection and sorting of classic algorithms

The basic principle of selection and sorting
1. Start from the first number
2. Traverse it again to find the subscript corresponding to the smallest number
3. Exchange position with the first number
4. Repeat the above steps from the second number

Selection sorting is similar to
bubble sorting. Bubble sorting means that a small bubble keeps changing into a big bubble floating on the water.
Selecting sorting is to directly find the subscript of the smallest bubble among all bubbles and send it directly to the front.

#include<stdio.h>
main()
{
    
    
    int a[100],j,i,n,k,t,l;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&a[i]);
    for(i=1;i<n;i++)
    {
    
    
        k=i;
        for(j=i+1;j<=n;j++){
    
      if (a[j]<a[k]) k=j;  }
        if(k!=i) {
    
    t=a[i];a[i]=a[k];a[k]=t; }
        for(l=1;l<=n;l++) printf("%d ",a[l]);
        printf("\n");
    }
}

Guess you like

Origin blog.csdn.net/qq_43249043/article/details/95885631
Recommended