选择排序(C语言)

采用先确定位置然后找数字的方法。

选定位置或者在最前或者在最后。

 把选定位置上的数字和所有其他数字做顺序调整直到把合适数字放在选定位置上。

/*

 * 插入排序

 * */

#include <stdio.h>

void insert_sort(int *p_num, int size)

{

   int num = 0, num1 = 0, tmp = 0;

   for (num = 1;num <= size - 1;num++)

    {

       //每次循环要把下标为num的存储区内容插入到前面合适的位置上

       for (num1 = num - 1;num1 >= 0;num1--)

       {

           //把下标为num1和num1+1两个存储区里的内容做顺序调整

           if (*(p_num + num1) > *(p_num + num1 + 1))

           {

                tmp = *(p_num + num1);

                *(p_num + num1) = *(p_num +num1 + 1);

                *(p_num + num1 + 1) = tmp;

           }

           else

           {

                break;

           }

       }

    }

}

int main()

{

   int arr[] = {31, 24, 45, 63, 18, 3, 14};

   int num = 0;

   insert_sort(arr, 7);

   for (num = 0;num <= 6;num++)

    {

       printf("%d ", arr[num]);

    }

   printf("\n");

   return 0;

}

猜你喜欢

转载自blog.csdn.net/uperficialyu/article/details/79018056