Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 21st, 2006, 9:51 PM   #31
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Where's your example and your explanation of how it beats O(n^2)??
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 21st, 2006, 10:10 PM   #32
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Visual Sorting Algorithm Comparison Demo
http://cg.scs.carleton.ca/~morin/misc/sortalg/

Thought this would be interesting for this thread.
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is online now   Reply With Quote
Old Dec 22nd, 2006, 7:13 AM   #33
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
This thread is dynamite, definitely worth the bookmark.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 6th, 2007, 2:39 AM   #34
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
here's a bucketsort that can sort negative numbers...really simple idea, multiplies the running time by about 4. ten million items from 0-999 in about 3 seconds...not bad.

driver
//total size of list
#define LISTSIZE 2000000
//range of nums within the list
#define RANGE 1000

//for printing to stdout
#include <iostream>
//for rand() & srand()
#include <cstdlib>
//to seed srand()
#include <ctime>
//for GetTickCount()
#include "windows.h"
//our actual class
#include "bucketsort.cpp"

using namespace std;

int main()
{
	//vars for tick count
	//correct microsoft data type is DWORD, but it works with long w/no difference
	long start, stop;

	//instantiate our object that does the work
	bucketsort<int> a;

	//this is our sample deque
	deque<int> s;

	//seed rand()
	srand(time(NULL));

	//fill our input deque with numbers from 1-RANGE
	/*for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back(rand() % RANGE);
	}*/

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back(rand() % RANGE);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back((rand() % RANGE) * -1);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back(rand() % RANGE);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back((rand() % RANGE) * -1);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back(rand() % RANGE);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back((rand() % RANGE) * -1);
	}

	for(int i = 0; i < LISTSIZE; i++)
	{
		s.push_back(rand() % RANGE);
	}
	
	cout<<"\nbefore sort:"<<endl;
	//a.print(s);

	cout<<"\nSorting...\n"<<endl;

	//start time
	start = GetTickCount();
	
	//sort the list
	a.splitsort(s);

	//stop time
	stop = GetTickCount();

	cout<<"\n\nafter sort:"<<endl;
	//a.print(s);

	//print time to sort
	cout<<"\ntime to sort is "<<stop - start<<" milliseconds."<<endl;

	return 0;
}

bucketsort
#include <deque>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

template <class T>
class bucketsort
{
public:
	void sort(deque<T> &);
	void splitsort(deque<T> &);
	void print(deque<T> &);
};

template <class T>
void bucketsort<T>::print(deque<T> & x)
{
	//instantiate our iterator
	std::deque<T>::iterator itr; 

	//from start to end cout<< the dereferenced iterator
	//(the value currently pointed at)
	for(itr = x.begin(); itr != x.end(); ++itr)
	{
		cout<<*itr<<" ";
	}
	cout<<endl;
}

template <class T>
void bucketsort<T>::sort(deque<T> & d)
{
	//save deque size to prevent calling this function more than once
	int size = d.size();

	//find the biggest element in the deque
	//value of (max_element returns a pointer so we dereference it
	double range = *max_element(d.begin(), d.end());

	T* unsorted = new T[size];

	for(int x = 0; x < size; x++)
	{
		unsorted[x] = d.front();
		d.pop_front();
	}

	//uncomment to see the biggest element DEBUGGING CODE
	//cout<<range<<"\n"<<endl;

	//looping vars
	int i = 0, j = 0;

	//our buckets (+1 so indices will match elements)
	deque<T> buckets(range + 1);

	//set all vals in buckets to zero
	//the first part of the loop is empty
	//b/c i already == zero, so we don't have to declare extra crap.
	for(; i < range; i++) buckets[i] = 0;
	
	//fill each bucket with vals
	//if a val within range exists increment that bucket
    for(i = 0; i < size; i++) ++buckets[unsorted[i]];

	//here's the crazy sorting part...
	//if the bucket is empty we move on,
	//else we push that onto our sorted deque
	//decrement each bucket until its empty
	//then we stick the next val into our deque
	//there's a lot of weird crap going on in this line
	//that could have been split up, but i like weird code
	for(i = 0; i < range + 1; i++) while(buckets[i]-- != 0) unsorted[j++] = i;

	for(int y = 0; y < size; y++) d.push_back(unsorted[y]);

	delete [] unsorted;


	//uncomment the below line if you want to see the buckets
	//and what they hold DEBUGGING CODE
	//for(int k = 0; k < range + 1; k++) cout<<buckets[k]<<" "<<endl;
}

template <class T>
void bucketsort<T>::splitsort(deque<T> & d)
{
	deque<T> neg;
	deque<T> pos;

	//fill up neg and pos
	while(!d.empty())
	{
		if(d.front() < 0)
		{
			//convert each element of neg to a positive # for bucketsort
			neg.push_back(-1 * d.front());
			d.pop_front();
		}
		else
		{
			//otherwise put the # in pos
			pos.push_back(d.front());
			d.pop_front();
		}
	}

	//sort both lists
	sort(neg);
	sort(pos);

	//put neg numbers back in
	while(!neg.empty())
	{
		//multiply each # of sorted neg deck by -1 to convert back to negative
		//and then push it back into the original deque from the back
		d.push_back(neg.back() * -1);
		neg.pop_back();
	}

	//put pos numbers back in
	while(!pos.empty())
	{
		d.push_back(pos.front());
		pos.pop_front();
	}
}
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Jun 25th, 2007, 9:09 AM   #35
cheap freelancer
Newbie
 
Join Date: Jun 2007
Location: askexpert.info
Posts: 4
Rep Power: 0 cheap freelancer is on a distinguished road
I have heard the best sort is post master sort as per time complexity is concerned, can anyone throw some light on it?
cheap freelancer is offline   Reply With Quote
Old Mar 3rd, 2008, 7:30 AM   #36
Chinmoy
Newbie
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0 Chinmoy is on a distinguished road
Re: 25 ways to efficiently sort an array/list of integers

sorting will work differently fro different type of data.if you have a small number ofsmall numbers than any of the basic sorts vis, selection,bubble or insertion will work.if its a large number of small datas then i would prefer quicksort.if the numbers to be sorted are lat\rge each then ill go for radix sort and if the data is the largest dianosaur you have ever seen then merge sort will do it justice..
Chinmoy is offline   Reply With Quote
Old Mar 3rd, 2008, 7:31 AM   #37
Chinmoy
Newbie
 
Join Date: Feb 2008
Posts: 2
Rep Power: 0 Chinmoy is on a distinguished road
Re: 25 ways to efficiently sort an array/list of integers

sorting will work differently fro different type of data.if you have a small number of small numbers than any of the basic sorts vis, selection,bubble or insertion will work.if its a large number of small datas then i would prefer quicksort.if the numbers to be sorted are lat\rge each then ill go for radix sort and if the data is the largest dianosaur you have ever seen then merge sort will do it justice..
Chinmoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sort and swap brad sue C 1 Mar 25th, 2006 7:33 AM
interpolation search on a million EverLearning C 24 Jun 15th, 2005 3:42 PM
threaded merge sort help AusTex C 1 May 1st, 2005 5:58 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC