归并和sort排序简单运用

Day03 排序

今天学习了归并排序和sort函数的简单运用,感觉sort函数真的是无比的神奇,能加各种神奇的东西,然后进行一些排序练习

归并排序基本代码

#include<iostream>
using namespace std;
void merge(int *data, int start, int end, int *result)
{
	if (start < end)//(end-start)>1
	{
		int mid = (end + start) / 2;
		merge(data, start, mid, result);
		merge(data, mid + 1, end, result);
		int i = start, j = mid + 1, k = 0;
		while (i <= mid && j <= end)
		{
			if (data[i] <= data[j])
				result[k++] = data[i++];
			else
				result[k++] = data[j++];
		}
		while (i <= mid)
		{
			result[k++] = data[i++];
		}
		while (j <= end)
		{
			result[k++] = data[j++];
		}
		for (int i = 0; i < k; i++)
			data[start + i] = result[i];
	}
}
int main()
{
	int data[] = { 3,6,5,8,1 };
	int result[10];
	merge(data, 0, 4, result);
	for (int i = 0; i < 5; i++)
		cout << result[i] << " ";
	system("pause");
	return 0;
}

A-Teams Forming

这道题用了sort函数和归并两种方法,归并函数比较难理解,写下代码,以后忘了方便找。归并重要还是要理解递归的调用。
代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
/*void merge(int *data, int start, int end, int *result)//归并
{
	if (start < end)//(end-start)>1
	{
		int mid = (end + start) / 2;
		merge(data, start, mid, result);
		merge(data, mid + 1, end, result);
		int i = start, j = mid + 1, k = 0;
		while (i <= mid && j <= end)
		{
			if (data[i] <= data[j])
				result[k++] = data[i++];
			else
				result[k++] = data[j++];
		}
		while (i <= mid)
		{
			result[k++] = data[i++];
		}
		while (j <= end)
		{
			result[k++] = data[j++];
		}
		for (int i = 0; i < k; i++)
			data[start + i] = result[i];
	}
}
int result[110];*/
int a[110];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	//merge(a, 0, n - 1, result);
	
	int counts = 0;
	sort(a, a + n);
	for (int i = 0; i < n; i += 2)
	{
		if (a[i] < a[i + 1])
		{
			while (a[i] < a[i + 1])
			{
				a[i]++;
				counts++;
			}
		}
	}
	/*for (int i = 0; i < n; i += 2)//归并
	{
		if (result[i] < result[i + 1])
		{
			while (result[i]<result[i+1])
			{
				result[i]++;
				counts++;
			}
		}
	}*/
	cout << counts;
	system("pause");
	return 0;
}

Where is the Marble?

这道题就是排序和检索
代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
int a[10010];
int main()
{
	int N, Q;
	int x = 1;
	while (cin>>N>>Q&&(N!=0 && Q!=0))
	{
		for (int i = 0; i < N; i++)
			cin >> a[i];
		sort(a, a + N);
		cout << "CASE# " << x << ":" << endl;
		bool find;
		while (Q--)
		{
			int b,index;
			cin >> b;
			find = false;
			for (int j = 0; j < N; j++)
			{
				if (a[j] == b)
				{
					find = true;
					index = j + 1;
					break;
				}
			}
			if (find)
				cout << b << " found at " << index << endl;
			else
				cout << b << " not found" << endl;
		}
		x++;
	}
	system("pause");
	return 0;
}

C-sort

这道题就是sort函数的运用,但是可能会超时,所以运用printf输出,printf比cout速度更快。还有cin的优化 std:ios::sync_with_stdio(false);
加入这行代码速度会和scanf差不多。

代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000000];
int main()
{
	int n, m;
	std::ios::sync_with_stdio(false);
	while (cin>>n>>m)
	{
		for (int i = 0; i < n; i++)
			cin >> a[i];
		sort(a, a + n);
		int count = 0;
		for (int i = n - 1; i >= 0; i--)
		{
			if (count == m||count ==n)
				break;
			printf("%d", a[i]);
			if (count == m - 1)
				printf("\n");
			else
			    printf(" ");
			count++;
		}
	}
	system("pause");
	return 0;
}

D-稳定排序

这道题是判断所给数据的排序是否对,以及是否稳定。所以必须找到一种稳定的排序方式。刚开始想到冒泡排序,但是却会超时。最后用了sort函数的排序sort(s,s+n,cmp);
代码如下:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct S
{
	string name;
	int score;
	int k;
}s1[310],s2[310];
int cmp(S a,S b)
{
	if (a.score == b.score)
		return a.k < b.k;
	return a.score > b.score;
}
int main()
{
	int N;
	while (cin>>N)
	{
		for (int i = 0; i < N; i++)
		{
			cin >> s1[i].name >> s1[i].score;
			s1[i].k = i;
		}
		for (int i = 0; i < N; i++)
		{
			cin >> s2[i].name >> s2[i].score;
			s2[i].k = i;
		}
		sort(s1, s1 + N, cmp);
		bool stable = false , answer = false;
		for (int i = 0; i < N; i++)
		{
			if (s1[i].score != s2[i].score)
			{
				answer = true;
				break;
			}
		}
		if (!answer)
		{
			for (int i = 0; i < N; i++)
			{
				if (s1[i].name != s2[i].name)
				{
					stable = true;
					break;
				}
			}
		}
		if (answer)
		{
			cout << "Error" << endl;
			for (int i = 0; i < N; i++)
				cout << s1[i].name << " " << s1[i].score << endl;
		}
		else
		{
			if (stable)
			{
				cout << "Not Stable" << endl;
				for (int i = 0; i < N; i++)
					cout << s1[i].name << " " << s1[i].score << endl;

			}
			else
				cout << "Right" << endl;
		}
	}
	system("pause");
	return 0;
}

F-EXCEL排序

这道题分为几种方式的排序,实际就是sort函数排序的运用。
代码如下:中间还有冒泡排序的写法

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct S
{
	string s1;
	string s2;
	int a;
}s[100000];
bool cmp1(S x, S y)
{
	return x.s1 < y.s1;
}
bool cmp2(S x, S y)
{
	if (x.s2 == y.s2)
		return x.s1 < y.s1;
	return x.s2 < y.s2;
}
bool cmp3(S x, S y)
{
	if (x.a == y.a)
		return x.s1 < y.s1;
	return x.a < y.a;
}
/*void first(S *s,int *a,int N)
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N - 1; j++)
		{
			if (s[j].s1 > s[j + 1].s1)
			{
				string temp1 = s[j].s1;
				s[j].s1 = s[j + 1].s1;
				s[j + 1].s1 = temp1;
				string temp2 = s[j].s2;
				s[j].s2 = s[j + 1].s2;
				s[j + 1].s2 = temp2;
				int temp3 = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp3;
			}
		}
	}
}*/
/*void second(S *s, int *a, int N)
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N - 1; j++)
		{
			if (s[j].s2 > s[j + 1].s2)
			{
				string temp1 = s[j].s1;
				s[j].s1 = s[j + 1].s1;
				s[j + 1].s1 = temp1;
				string temp2 = s[j].s2;
				s[j].s2 = s[j + 1].s2;
				s[j + 1].s2 = temp2;
				int temp3 = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp3;
			}
			if (s[j].s2 == s[j + 1].s2)
			{
				if (s[j].s1 > s[j + 1].s1)
				{
					string temp1 = s[j].s1;
					s[j].s1 = s[j + 1].s1;
					s[j + 1].s1 = temp1;
					string temp2 = s[j].s2;
					s[j].s2 = s[j + 1].s2;
					s[j + 1].s2 = temp2;
					int temp3 = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp3;
				}
			}
		}
	}
}*/
/*void third(S *s, int *a, int N)
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N - 1; j++)
		{
			if (a[j] > a[j+1])
			{
				string temp1 = s[j].s1;
				s[j].s1 = s[j + 1].s1;
				s[j + 1].s1 = temp1;
				string temp2 = s[j].s2;
				s[j].s2 = s[j + 1].s2;
				s[j + 1].s2 = temp2;
				int temp3 = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp3;
			}
			if (s[j].s2 == s[j + 1].s2)
			{
				if (s[j].s1 > s[j + 1].s1)
				{
					string temp1 = s[j].s1;
					s[j].s1 = s[j + 1].s1;
					s[j + 1].s1 = temp1;
					string temp2 = s[j].s2;
					s[j].s2 = s[j + 1].s2;
					s[j + 1].s2 = temp2;
					int temp3 = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp3;
				}
			}
		}
	}
}*/
//int a[100000];
int main()
{
	int N, C;
	//S s[10000];
	int x = 1;
	while (cin>>N>>C && (N!=0))
	{
		for (int i = 0; i < N; i++)
			cin >> s[i].s1 >> s[i].s2 >> s[i].a;
		if (C == 1)
			sort(s, s + N, cmp1);
		//first(s,a N);
		else if (C == 2)
			sort(s, s + N, cmp2);
		//second(s, a, N);
		else if (C == 3)
			sort(s, s + N, cmp3);
			//third(s, a, N);
		cout << "Case " << x << ":" << endl;
		for (int i = 0; i < N; i++)
			cout << s[i].s1 << " " << s[i].s2 << " " << s[i].a<<endl;
		x++;
	}
	return 0;
}
今天做了这几道题目感觉学到了不少,了解了递归和sort函数的运用,算是排序勉强入门吧,还需继续努力。排序重要还是要理解原理,还要回灵活运用。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值