对链表进行排序将会是O(N^2)或需要外部随机访问存储。
向量和数组具有随机访问存储。排序可以是O(NlogN)。
在1000个元素时,您将开始看到O(N^2)和O(NlogN)之间的差异。在100万个元素时,您一定会注意到这种差异!
在非常特殊的情况下,可能会获得O(N)排序。(例如:对扑克牌进行排序。我们可以创建一个函数(card),将每张卡映射到其排序位置。)
但是一般来说,O(NlogN)就是最好的了。所以你最好使用STL的sort()!
只需添加#include <algorithms>
您只需要添加一个operator<()或一个sort functor。
但是一个建议:天哪,如果您要按日期排序,请将其编码为表示自纪元以来的秒数的long int (mktime?),或者至少使用"年/月/日-小时:分钟:秒.分数"格式。(并确保所有内容都是带前导零的2位(或4位)数字!)比较"6/22/2008-4:34"和"12/5/2007-14:54"将需要解析!比较"2008/06/22-04:34"和"2007/12/05-14:54"更容易。(尽管比较两个整数仍然要低效得多!)
Rich写道:
其他答案似乎更多地涉及语法,这正是我所缺乏的。
好的。对于基本的"int"类型,我们有:
#define PRINT(DATA,N) for(int i=0; i<N; i++) { cout << (i>0?", ":"") << DATA[i]; } cout << endl;
int
main()
{
int d [10] = { 1, 4, 0, 2, 8, 6, 3, 5, 9, 7 };
PRINT(d,10);
sort( d, d+10 );
PRINT(d,10);
cout << endl;
int eData [10] = { 1, 4, 0, 2, 8, 6, 3, 5, 9, 7 };
vector<int> e;
for(int i=0; i<10; i++ )
e.push_back( eData[i] );
PRINT(e,10);
sort(e.begin(), e.end());
PRINT(e,10);
}
使用自己的类型,我们可以:
class Data
{
public:
string m_PackLine;
string m_FileDateTime;
int m_NumberDownloads;
Data( const string & thePackLine = "",
const string & theDateTime = "",
int theDownloads = 0 )
: m_PackLine ( thePackLine ),
m_FileDateTime ( theDateTime ),
m_NumberDownloads ( theDownloads )
{ }
void set( const string & thePackLine,
const string & theDateTime,
int theDownloads = 0 )
{
m_PackLine = thePackLine;
m_FileDateTime = theDateTime;
m_NumberDownloads = theDownloads;
}
ostream & operator<<( ostream & theOstream ) const
{
theOstream << "PackLine=\"" << m_PackLine
<< "\" fileDateTime=\"" << m_FileDateTime
<< "\" downloads=" << m_NumberDownloads;
return theOstream;
}
bool operator< ( const Data & theOtherData ) const
{ return m_FileDateTime < theOtherData.m_FileDateTime; }
};
ostream & operator<<( ostream & theOstream, const Data & theData )
{ return theData.operator<<( theOstream ); }
#define PRINT(DATA,N) for(int i=0; i<N; i++) { cout << "[" << i << "] " << DATA[i] << endl; } cout << endl;
int
main()
{
Data d [10];
d[0].set( "Line 1", "2008/01/01 04:34", 1 );
d[1].set( "Line 4", "2008/01/04 04:34", 4 );
d[2].set( "Line 0", "2008/01/00 04:34", 0 );
d[3].set( "Line 2", "2008/01/02 04:34", 2 );
d[4].set( "Line 8", "2008/01/08 04:34", 8 );
d[5].set( "Line 6", "2008/01/06 04:34", 6 );
d[6].set( "Line 3", "2008/01/03 04:34", 3 );
d[7].set( "Line 5", "2008/01/05 04:34", 5 );
d[8].set( "Line 9", "2008/01/09 04:34", 9 );
d[9].set( "Line 7", "2008/01/07 04:34", 7 );
PRINT(d,10);
sort( d, d+10 );
PRINT(d,10);
cout << endl;
vector<Data> e;
e.push_back( Data( "Line 1", "2008/01/01 04:34", 1 ) );
e.push_back( Data( "Line 4", "2008/01/04 04:34", 4 ) );
e.push_back( Data( "Line 0", "2008/01/00 04:34", 0 ) );
e.push_back( Data( "Line 2", "2008/01/02 04:34", 2 ) );
e.push_back( Data( "Line 8", "2008/01/08 04:34", 8 ) );
e.push_back( Data( "Line 6", "2008/01/06 04:34", 6 ) );
e.push_back( Data( "Line 3", "2008/01/03 04:34", 3 ) );
e.push_back( Data( "Line 5", "2008/01/05 04:34", 5 ) );
e.push_back( Data( "Line 9", "2008/01/09 04:34", 9 ) );
e.push_back( Data( "Line 7", "2008/01/07 04:34", 7 ) );
PRINT(e,10);
sort(e.begin(), e.end());
PRINT(e,10);
}