【箱排序】( 链表实现 )

int main( void ){
	std::list< studentRecord > list;
	list.emplace_back( 42953, "F" ); //student F has score 42953
	list.emplace_back( 32452, "A" ); //student A has score 32452
	list.emplace_back( 12134, "E" ); //...
	list.emplace_back( 24523, "H" );
	list.emplace_back( 33413, "J" );
	list.emplace_back( 12534, "U" );
	list.emplace_back( 36462, "M" );
	binSort( list, 50000 ); // sort by score
	for( auto& stu : list ) std::cout << stu << std::endl;
	return 0;
}

【箱排序】( 链表实现 )_第1张图片

 

typedef struct studentRecord{
	int score;
	std::string name;
	operator int() const { return score; }
	bool operator!=( const studentRecord& other ) const
	{ return !( score == other.score ); }
} studentRecord;
std::ostream& operator << ( std::ostream& os, const studentRecord& stu ){
	return ( os << stu.score
				<< " "
				<< stu.name
				<< std::endl );
}

void binSort(std::list< studentRecord >& list, int range ){
	auto bottom
		 = new std::remove_reference< decltype( list ) >::type[ range + 1 ];
	for( auto& elem : list )
		bottom[ ( int )elem ].push_back( elem  );
	list.clear();

	for_each( bottom, bottom + range + 1, [&list]( decltype( list ) blist ){
		for( auto& elem : blist )
			list.push_back( elem );
	});
	
	delete[] bottom;
}

你可能感兴趣的:(算法,链表,windows,数据结构,stl,c++,算法)