简体   繁体   中英

Is it faster to read then sort or to sort while reading an array?

I'm programming in Pascal.

Is it faster to read an array, and then sort it (using, say, quick sort), or to sort it while reading it? I won't need the unsorted array anymore, so I can change the order of array as I read it.

Sorting the entire array at the end should be the default choice.

You gain absolutely nothing by incrementally sorting the array as you're reading it. What you're losing, however, is the flexibility of choosing the best sorting algorithm for the job.

See this: Average time complexity of quicksort vs insertion sort

As if you are sorting while reading it is going to be insertion sort the question is which sort is faster.

Reading only takes o(n) time and that is shorter then the sorting complexity (O(nlogn)thus making the reading insignificant

Read the whole array, then sort it. Otherwise you will need to use heap to keep it in sorted structure during reading or any other structure which will take more space & time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM