#include <stdio.h> void BubbleSort(int *a, int len) { int count,i,j,min_temp,temp_int; for (count=0;count<len-1;count++)//总共要进行len-1步排序 { //每一次都要找到剩下的元素的最小元素,并将它放在剩下元素的开始位置 min_temp=count; for (i=count+1;i<len;i++)//注意,从count+1开始比较 { if (a[min_temp]>a[i]) { min_temp=i; } } if (count!=min_temp)//与最小的元素交换位置 { temp_int=a[count]; a[count]=a[min_temp]; a[min_temp]=temp_int; } printf("第%d步排序结果是:",count+1); for(j=0;j<len;j++ ) { printf("%d ",a[j]); } printf("\n"); } } void main() { int a[10]={15,26,11,56,45,82,13,20,99,17}; BubbleSort(a,10); }