1. 程式人生 > >常見排序之氣泡排序

常見排序之氣泡排序

#include<stdio.h>

int Array[] = {2,0,1,3,4,8,6,7,5,9};

void Bubble_Sort(int *Array_Sort)注:此處可寫int Array_Sort[10]

{

      int i,j;

      int temp;

      for(i = 0; i < 10-1; i++)

      {

           for(j = 0; j < 10-i-1; j++)

           {

                 if(Array_Sort[j]>Array_Sort[j+1]) 注:此處改為<變為大到小排列

                 {

                      temp = Array_Sort[j];

                      Array_Sort[j] = Array_Sort[j+1];

                      Array_Sort[j+1] = temp;

                 }

           }

      }

}

 

int main(void)

{

      int j = 0;

      Bubble_Sort(Array);

      for(j = 0; j < 10; j++)

      {

           printf("%d\r\n",Array[j]);

      }

      return 0;

}