您的位置:首页 > 其它

poj_1007 排序水题

2015-01-27 11:34 232 查看
code:

<span style="font-size:18px;">#include <iostream>
#include <fstream>

using namespace std;

void quick_sort(char s[][51],int sort[],int left,int right)
{
	if (left < right)
	{
		int i = left, j = right, x = sort[i];
		char temp[51];
		strcpy(temp,s[i]);
		while (i < j)
		{
			while (i<j&&sort[j]>x)
				j--;
			if (i < j)
			{
				// i <-> j
				strcpy(s[i], s[j]);
				sort[i++] = sort[j];
			}
			while (i<j&&sort[i]<x)
				i++;
			if (i < j)
			{
				// i <-> j
				strcpy(s[j],s[i]);
				sort[j--] = sort[i];
			}
		}
		sort[i] = x;
		strcpy(s[i], temp);
		quick_sort(s, sort, left, i-1);
		quick_sort(s, sort, i+1, right);
	}
}

int main()
{
	fstream in("input.txt");
	int n, m;
	char s[100][51];
	int sort[100];
	cin >> n >> m;
	memset(sort,0,sizeof(sort));
	for (int i = 0; i < m; i++)
	{
		cin >> s[i];
	}
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			for (int k = j + 1; k < n; k++)
			{
				if (s[i][j]>s[i][k])
					sort[i]++;
			}
		}
	}
	//quick_sort
	quick_sort(s,sort,0,m-1);
	for (int i = 0; i < m; i++)
		cout << s[i] << endl;
	//system("pause");
	return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: