排序算法——冒泡排序

 1 void BubbleSort(int unsort[],const int count)

 2 {

 3     for (int i = 0; i < count-1;i++)

 4     {

 5         for (int j = 0;j<count-1-i;j++)

 6         {

 7             int temp;

 8             if (unsort[j]>unsort[j+1])

 9             {

10                 temp = unsort[j];

11                 unsort[j] = unsort[j + 1];

12                 unsort[j + 1] = temp;

13             }

14         }

15     }

16 }

测试

 1 int main()

 2 {

 3     int unsort[] = {2,5,7,4,6,9};

 4     BubbleSort(unsort, 6);

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

 6     {

 7         printf("%d\r",unsort[i]);

 8     }

 9     return 0;

10 }

 

你可能感兴趣的:(冒泡排序)