写一主函数输入一数组,写一子函数实现对该数组的冒泡排序并输出。

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include

#define N 6
void bubblesort(int *p)//从小到大排序
{
	for (int i = 0; i < N; i++)
		for (int j = N - 1; j >= i; j--)
		{
			if (*(p + j - 1)>*(p + j))
			{
				static int k = 0;
				k = *(p + j - 1);
				*(p + j - 1) = *(p + j);
				*(p + j) = k;
			}
		}
		for (int i = 0; i< N; i++)
		{
			printf("%-3d", *(p + i));
		}
}
void main()
{
	srand((unsigned)time(NULL));
	int a[N] = { rand() % 100, rand() % 100, rand() % 100, rand() % 100, rand() % 100, rand() % 100 };
	//int a[N] = { 1, 1, 2, 3, 6, 3 };
	bubblesort(a);
	system("pause");
}
写一主函数输入一数组,写一子函数实现对该数组的冒泡排序并输出。_第1张图片

你可能感兴趣的:(cc++编程)