C-冒泡排序

#include <stdio.h>

int main()
{
    
    
	int a[] = {
    
    0,22,32,21,21,212,323,343,323,2121,1222};
	//计算出数组的长度
	int count = sizeof(a) / sizeof(a[0]);
	//交换数据的变量
	int t;
	//排序是数据间两两比较,所以运算次数比数组长度少一个
	for (int i = 0; i < count-1; i++) {
    
    
		for (int j = 0; j < (count-1) - i; j++) {
    
    
			if (a[j]>a[j+1]) {
    
    
				t = a[j];
				a[j] = a[j + 1];
				a[j + 1] = t;
			}
		}
	}

	printf("排序后的结果");
	//遍历数组
	for (int i = 0; i < count; i++) {
    
    
		printf("%d\n",a[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Willow_Spring/article/details/112847730
C-