Pogledaj jedan post
Old 10.04.2004., 23:03   #10
Kod:
/************************************************ 
** Copyright (C) 2004 [email protected]. All rights reserved. :D
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL
** 
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.opensource.org/licenses/gpl-license.php for GPL licensing information.
*/

#include <stdlib.h>
#include <string.h>
#include <iostream.h>

typedef unsigned long int ULINT;

template<typename T>
class LinearSort
{
	ULINT order[sizeof(T)][256];

public:
	void sort(T* data, ULINT len )
	{
		int h = 0, t = 0;

		memset( order, 0, sizeof(order) );

		for( ULINT i=0; i<len; i++ )
		{
			T num = data[i];

			if( num < 0 ) t++;

			for(int j=0; j<sizeof(T); j++)
			{
				order[j][num&0xFF] ++;
				
				num >>= 8;
			}
		}

		int elem[sizeof(T)] = { 0 };

		for( i=0; i<len; i++ )
		{
			T num = 0;

			for(int j=sizeof(T)-1; j>=0; j--)
			{
				num <<= 8;

				while(! order[j][elem[j]] )
				{
					elem[j] ++;
				}

				order[j][elem[j]] --;

				num |= elem[j];
			}

			num < 0 ? data[h++] = num : data[t++] = num;
		}
	}
};

int main()
{
	const int size = 8;

	int data[size] = { 12435438, -233456, 343456, -1324234, 0 ,5436435 ,-324522 ,-4};

	LinearSort<int> linear;

	linear.sort( data, size );

	for( int i=0; i<size; i++ )
		cout << data[i] << endl;

	return 0;
}
:
bizz is offline  
Odgovori s citatom