Answer the question
In order to leave comments, you need to log in
Program time 2.062 need 2 sec, how to do?
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
int N;
fstream fin, fout;
fin.open("input.txt", ios::in);
fout.open("output.txt", ios::out);
fin>>N;
int *A= new int[N];
for(int i=0;i<N;i++)
{
fin>>A[i];
}
sort(A,A+N);
for(int i=0;i<N;i++)
{
fout<<A[i]<<" ";
}
delete [] A;
fin.close();
fout.close();
return 0;
}
Answer the question
In order to leave comments, you need to log in
You can save a little if you read the entire file into the buffer at once, and drive it into the array via std::stringstream or even by hand. Similarly, with writing - first convert to a text buffer, then write in bulk to a file in one operation. Open the file as binary.
Perhaps if you apply a different sorting algorithm, the same will increase performance. The standard library is far from the fastest algorithm.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question