Answer the question
In order to leave comments, you need to log in
Which sort is the fastest?
If I understand everything correctly, and often the opposite happens ...
I have an array of 100,000 elements, each of them is a random number from 1 to 100,000;
What algorithm (that you know) can sort this entire array in the shortest time?
And the most important question:
How long will it take the algorithm to solve this problem?
Please don't answer with answers like (n log n), (n^2), etc. You need a specific time to kill the misunderstanding in yourself.
PS Thank you in advance for your patience with these questions.
Answer the question
In order to leave comments, you need to log in
If there is memory for another array of 100,000 elements, then counting sort will work the fastest:
void CountSort(int *A1,int *A2,int L,int Nmax){
for(int i=0;i<=Nmax;i++ )A2[i]=0;
for(int i=0;i < L;i++) A2[A1[i]]++;
int s=0;
for(int i=0;i<=Nmax;i++) for(int j=0;j < A2[i];j++) A1[s++]=i;
}
Here A1 is an array to be sorted, A2 is an additional array of length Nmax+1.
I got an average time of 0.88 milliseconds. For comparison, std::sort gives 7.2 milliseconds on the same examples - 8 times more.
Perhaps a radix sort modulo 256 will give even less time (it is not as aggressive with memory). But it won't fit in 4 lines.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question