sort array of integers lexicographically C++

前端 未结 12 1497
野的像风
野的像风 2021-02-02 13:53

I want to sort a large array of integers (say 1 millon elements) lexicographically.

Example:

input [] = { 100, 21 , 22 , 99 , 1  , 927 }
sorted[] = { 1           


        
12条回答
  •  醉话见心
    2021-02-02 14:15

    Use std::sort() with a suitable comparison function. This cuts down on the memory requirements.

    The comparison function can use n % 10, n / 10 % 10, n / 100 % 10 etc. to access the individual digits (for positive integers; negative integers work a bit differently).

提交回复
热议问题