On 8/3/2010 4:57 AM, Vivek wrote:
> --- In [email protected], John Gaughan<j...@...>  wrote:
>    
>> http://www.cplusplus.com/reference/algorithm/sort/
>>
>> I can sort in one line of code!
>>      
> John's suggestion is more elegant and maintainable - Something
> you should strive for as professional developers.
>    

Stupid me just realized there is an even more optimal solution: use a 
container that is intrinsically sorted, such as a binary tree:

http://www.cplusplus.com/reference/stl/set/

This way, you never need a sorting algorithm and don't even need to use 
the standard library's sort.

The time required for inserting into a binary tree is O(log2(n)), while 
quicksort is O(n log2(n)). So not using a sort algorithm at all is 
actually more optimal: you won't find a sorting algorithm that can beat 
the binary tree for a random data set outside of highly specialized 
applications.

-- 
John Gaughan
http://www.johngaughan.net/

Reply via email to