算法系列之排序(一):冒泡排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fx_odyssey/article/details/80109569

学习有感乏力,开始记录个人一步步学习的东西,先从最基础的开始。。。

何为冒泡?就是巴达兽在水底吐出的泡泡(手动笑)。泡泡上升的过程中,类比数组中最小数通过一次次循环逐步往前排。

#include <stdio.h>

int main()
{
	int a[10] = {2, 5, 9, 1, 4, 8, 6, 7, 3, 0};
	for (int i = 0; i < 10; i++)
	{
		printf("%d\t", a[i]);
	}
	printf("\n");
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9 - i; j++)
		{
			int t = 0;
			if (a[j] > a[j + 1])
			{
				t = a[j];
				a[j] = a[j + 1];
				a[j+1] = t;
			}
		}
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d\t", a[i]);
	}
	printf("\n");
	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fx_odyssey/article/details/80109569