下面是一个简单冒泡法排序,代码如下:
- #include <stdio.h>
- #define LEN 10
- int main()
- {
- int a,i,j;
- int ARRAY[10]={23,1,4,9,6,17,24,56,98,72};
- printf("Display this array:\n");
- for(a=0;a<10;a++)
- {
- printf("%d ",ARRAY[a]);
- }
- printf("\n");
- for(j=0;j<10;j++)
- {
- for(i=0;i<LEN-j-1;i++)
- {
- int temp;
- if(ARRAY[i]>ARRAY[i+1])
- {
- temp=ARRAY[i+1];
- ARRAY[i+1]=ARRAY[i];
- ARRAY[i]=temp;
- }
- }
- }
- printf("After sorting,the array is:\n");
- for(a=0;a<LEN;a++)
- {
- printf("%d ",ARRAY[a]);
- }
- printf("\n");
- return 0;
- }
下面是执行结果:
- fs@ubuntu:~/qiang/tmp$ ./5
- Display this array:
- 23 1 4 9 6 17 24 56 98 72
- After sorting,the array is:
- 1 4 6 9 17 23 24 56 72 98
- fs@ubuntu:~/qiang/tmp$