| | |
Sorting huge amount of numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Gotta love the SWAP macro too...
c++ Syntax (Toggle Plain Text)
#define SWAP(a, b) __typeof__(a) temp; temp = a; a = b; b = temp
That macro invites death without warning.
If you want to swap something, #include <algorithm> and use std::swap().
@cecyl
Generic algorithms will work just fine, but if you know something about the probable distribution of your data then you can reduce sort time considerably.
If you know that you have a relatively small set of values, then you can reduce your sort time to constant or near-constant time by simply counting how many times each value appears, then writing them back out in order.
Good luck.
[edit] Man, get up to visit the bathroom and someone smart answers first...
If you want to swap something, #include <algorithm> and use std::swap().
@cecyl
Generic algorithms will work just fine, but if you know something about the probable distribution of your data then you can reduce sort time considerably.
If you know that you have a relatively small set of values, then you can reduce your sort time to constant or near-constant time by simply counting how many times each value appears, then writing them back out in order.
Good luck.
[edit] Man, get up to visit the bathroom and someone smart answers first...
Last edited by Duoas; Jun 16th, 2008 at 12:07 pm.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> I have to sort it much, much more quicker, are 10 seconds possible?.
with 10 seconds at your disposal, you can sort it any which way you like.
the fastest way would perhaps be to use
it doesn't really matter if you are sorting a file; i/o would dominate.
here are some measurements of a sample run on a sequence of 500000 integers (roughly 30% of them have value 1, 20% are 2, 10% 3, remainder are [3,1000] ):
a. std::sort on the sequence: 0.0390625 seconds
b. std::partition for 1, 2 and 3, std::sort for the rest: 0.0234375 seconds
c. same as b, but read from a file sort and write to another file: 0.335938 seconds
perhaps, Salem's suggestion of using a command line sort program is all that you need.
with 10 seconds at your disposal, you can sort it any which way you like.
the fastest way would perhaps be to use
std::partition to move the most frequently occurring values (which are also the smallest values) to the front of the sequence. sort the remaining 40% or so of the sequence using a std::sort . it doesn't really matter if you are sorting a file; i/o would dominate.
here are some measurements of a sample run on a sequence of 500000 integers (roughly 30% of them have value 1, 20% are 2, 10% 3, remainder are [3,1000] ):
a. std::sort on the sequence: 0.0390625 seconds
b. std::partition for 1, 2 and 3, std::sort for the rest: 0.0234375 seconds
c. same as b, but read from a file sort and write to another file: 0.335938 seconds
C++ Syntax (Toggle Plain Text)
>dmesg | grep CPU: CPU: Intel(R) Pentium(R) 4 CPU 2.80GHz (2800.10-MHz 686-class CPU)
![]() |
Other Threads in the C++ Forum
- Previous Thread: Pls help with thezis!!A small Q.
- Next Thread: C++ Stack/GeneralTree help
Views: 698 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






