常见排序之直接插入排序

#include <stdio.h>

void Direct_Insert_Sort(int *Array)

{

      int i,j,temp;

      for(i = 1; i < 5; i++)

      {

           if(Array[i]<Array[i-1])

           {

                 temp = Array[i];

                 for(j = i-1; j>=0 && Array[j] > temp; j--)

                 {

                      Array[j+1] = Array[j];

                 }

                 Array[j+1] = temp;//此处因为j = -1

           }

      }

}

int main(void)

{

      int i;

      int Array_Sort[] = {3,2,4,5,8};

      Direct_Insert_Sort(Array_Sort);

      for(i = 0; i < 5; i++)

      {

           printf("%d\r\n",Array_Sort[i]);

      }

      return 0;

}

posted @ 2018-12-08 18:32  请叫我晓风哥哥  阅读(78)  评论(0编辑  收藏  举报