冒泡排序,数组,C++

越往下学就越发现,真的是基础不牢地动山摇,打算重新复习一下基础,花了半小时看了冒泡排序的理论,几分钟敲了代码

#include <iostream>
#define max 20
using namespace std;

void Bubble_sort(int A[],int n)
{
	int temp;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n-1-i;j++)
		{
			if(A[j]>A[j+1])
			{
				temp = A[j];
				A[j] = A[j+1];
				A[j+1] = temp;
			}
		}
	}
}

int main()
{
	int A[max];
	int n;
	int i;
	cout<<"输入数组长度:"<<endl;
	cin>>n;
	cout<<"输入数字元素:"<<endl;
	for(i=0;i<n;i++)
		cin>>A[i];
	Bubble_sort(A,n);
	cout<<"排序后:"<<endl;
	for(i = 0;i<n;i++)
		cout<<A[i]<<" ";
	cout<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/little_Muxue/article/details/121003914
今日推荐