Z
Z
zdvurechensky2018-05-20 20:10:10
C++ / C#
zdvurechensky, 2018-05-20 20:10:10

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

1 answer(s)
R
res2001, 2018-05-20
@res2001

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 question

Ask a Question

731 491 924 answers to any question