如何编程实现

guchonglin 2008-10-07 08:00:10
输入n个数,输出从小到大排序结果,但是要求输入的数据要10000个,从一个.txt文件读入,输出结果存在新的.txt文件中
例如输入:
8
1 8 3 9 4 4 5 5 7
输出
1 3 4 4 5 5 7 8 9
但都是以文件.tet形式输入和输出
而且想输几个就几个,假设有一个10000数的.tet
如何最快实现排序
...全文
48 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanjunwu 2008-10-07
  • 打赏
  • 举报
回复
用 STL.很容易实现.你就可以不用考虑算法了.
zclever 2008-10-07
  • 打赏
  • 举报
回复
想输几个输几个,这个要考虑大数组了。可以分成几块,块内排序,然后再两两归并排序,如此下去。
richbirdandy 2008-10-07
  • 打赏
  • 举报
回复
数不多 可以内排序
chlaws 2008-10-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

int main()
{
ofstream cre("cre.txt");

int n;
cout << "n :";
cin >> n;
int i = 0;
int *iptr=new int [n];
while(i<n && cin>>iptr[i++]);
copy(iptr,iptr+n,ostream_iterator<int>(cre," "));
delete iptr;
cre.close();

i = 0;
int inum,tlen;
cout << "input you want read number amount :";
cin >> inum;
tlen = inum;
iptr = new int[inum];
ifstream in("cre.txt");
while(!in.eof() && tlen--)
{
in >>iptr[i++];
}
in.close();

ofstream out("out.txt");
sort(iptr,iptr+inum);
copy(iptr,iptr+inum,ostream_iterator<int>(out," "));
out.close();

delete iptr;
return 0;
}
/*result:
n :8
1
9
5
6
4
8
2
7
input you want read number amount :6
Press any key to continue

cre.txt:
1 9 5 6 4 8 2 7
out.txt:
1 4 5 6 8 9
*/

effective_person 2008-10-07
  • 打赏
  • 举报
回复
要看你的序列了,如果你的序列是无须的话,最好的是用快速排序了!

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧