冒泡排序的简单实现

void sort(int *p , int len)
{
	int j;
	for (int i = 0; i < len-1; ++i)
	{
		for (j = 0; j < len-1-i; ++j)
		{
			if (p[j] > p[j+1])
			{
				swap(p[j],p[j+1]);
			}
		}
	}
}
#include <iostream>
using namespace std;

void print(int *p , int len)
{
	for (int i = 0; i < len; ++i)
	{
		cout << p[i] << ' ';
	}
	cout << endl;
}

void swap(int *a , int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void sort(int *p , int len)
{
	int j;
	for (int i = 0; i < len-1; ++i)
	{
		for (j = 0; j < len-1-i; ++j)
		{
			if (p[j] > p[j+1])
			{
				swap(p[j],p[j+1]);
			}
		}
	}
}

int main(int argc, char const *argv[])
{
	int a[] = {2,5,7,8,32,1,81,23,0};
	int len = sizeof(a)/sizeof(a[0]);
	print(a,len);
	sort(a,len);
	print(a,len);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yinzewen123/article/details/81138661