`
xitongyunwei
  • 浏览: 926505 次
文章分类
社区版块
存档分类
最新评论

冒泡排序源代码

 
阅读更多
#include <iostream>
#include <iomanip>
#include <cmath>
#define PI 3.1415927
using namespace std;

void bubbleSort(int *a, int size) {
	for(int i=0; i<size-1; i++) { //circle times
		for(int j=0; j<size-1-i; j++) { //compare times
			if(a[j]>a[j+1]) {
				int tmp = a[j];
				a[j] = a[j+1];
				a[j+1] = tmp;
			}
		}
	}
}

int main()
{
	int n, m;
	while(cin>>n>>m) {
		if(n==0&&m==0) break;
		int *a = new int[n];
		int *b = new int[m];
		for(int i=0; i<n; i++) cin >> a[i];
		for(int i=0; i<m; i++) cin >> b[i];
		bubbleSort(a, n);
		bubbleSort(b, m);
		int count = 0;
		for(int i=0; i<n; i++) {
			bool flag = false;//flag: whether a[i] appears in b
			for(int j=0; j<m; j++) {
				if(a[i]==b[j]) {
					flag = true;
					break;
				}
			}
			if(!flag) { count++; ;cout << a[i] << " ";}
		}
		if(count==0) cout << "NULL" << endl;
		else cout << endl;
	}
	
	return 0;
}


上面的源代码来源于HDUOJ2034。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics