Difference selection sort, and bubble sort

#include <stdio.h>
#define N 10
int main()
{//从大到小
    int a[N]={2,17,8,3,24,53,82,1,29,101};
    int i,j,k,t;
    for(i=0;i<9;i++)
    {       k=i;//a
        for(j=i+1;j<10;j++)
        {    //  b    a
            if(a[j] > a[k])    //这是拿固定元素来比较,是选择排序
                                   //如果k没有变化,说明是最大的,不需要换所以应该取反
        {
            t=a[j];//交换两个位置 a[i]和最后一个比他大的换位置,后面再换
            a[j]=a[i];
            a[i]=t;
        }
        }
    }
    for(i=0;i<9;i++)
        printf("%d ",a[i]);
    return 0;
}


Select Sort
k will not change (because k determine which one ring), means that this element has been to take a position on his back and every element of comparison, if younger than him, will change the number, then proceed relatively, that is, one down, you can directly biggest on the front.
// equivalent to first provide for the ring

Bubble Sort
except for the last element to be compared, by comparing the cycle of the second layer, each comparison and commutation position to be determined, most times every time to be determined, the number of maximum or minimum in the final , two adjacent elements of comparison (suddenly said very clearly bubble sort)

Published 18 original articles · won praise 0 · Views 203

Guess you like

Origin blog.csdn.net/weixin_46456339/article/details/105145487