C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 23rd, 2005, 10:07 AM
Voelcker's Avatar
Voelcker Voelcker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Maryland
Posts: 225 Voelcker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 51 m 59 sec
Reputation Power: 9
Quicksort array of class objects

I'm working on yet another class project. To give some back ground, I have to modify a previous project to sort a dynamically sized array using a quicksort algorithm, and the quicksort function must be a template function so it could sort arrays of any data type.

The original project involved reading in a list of names from a file and adding them to a dynamically sized array of class objects. Each array item is an instance of class Name. That project works flawlessly and I'm not as concerned with the original project as I am changing the implementation to sort the array of class objects.

On the class discussions there is a quicksort function and swap function that looks like:
Code:
void Swap ( int &First, int &Second )
{
	int Temp = First;
	First = Second;
	Second = Temp;
}

void QuickSort  ( int Data[], int Size )
{
	if ( Size > 1 )
	{
		if ( Size == 2 )
		{
			if ( Data[0] > Data[1] )
				Swap ( Data[0], Data[1] );
		}
		else
		{
			int Pivot = Size / 2, Partition1 = -1, Partition2 = Size;
			while ( Partition1 < Pivot && Data[++Partition1] <= Data[Pivot] )
				;
			while ( Partition2 > Pivot && Data[--Partition2] > Data[Pivot] )
				;
			while ( Partition1 != Pivot || Partition2 != Pivot )
			{
				swap ( Data [Partition1], Data [Partition2] );				
				if ( Partition1 == Pivot )
				{
					Pivot = Partition2;
					while ( Partition1 < Pivot &&
							     Data[++Partition1] <= Data[Pivot] )
						;
				}
				else if ( Partition2 == Pivot )
				{
					Pivot = Partition1;
					while ( Partition2 > Pivot &&
							     Data[--Partition2] > Data[Pivot] )
						;
				}
				else
				{
					while ( Partition1 < Pivot &&
							     Data[++Partition1] <= Data[Pivot] )
						;
					while ( Partition2 > Pivot &&
							     Data[--Partition2] > Data[Pivot] )
						;
				}
			}
			QuickSort ( Data, Pivot );
			QuickSort ( &Data[Pivot+1], Size - 1 - Pivot );
		}
	}
}
Normally, I'd change the 'int' types in the definitions and prototypes to 'Name' (the name of the class) where necesary, but since I have to define the function to sort arrays of any type, I figured I'd make it a template function:
Code:
template <class Type>
void Swap ( Type &First, Type &Second )
{
	int Temp = First;
	First = Second;
	Second = Temp;
}

template <class Type>
void QuickSort  ( Type Data[], int Size )
{
	if ( Size > 1 )
	{
		if ( Size == 2 )
		{
			if ( Data[0] > Data[1] )
				Swap ( Data[0], Data[1] );
		}
		else
		{
			int Pivot = Size / 2, Partition1 = -1, Partition2 = Size;
			while ( Partition1 < Pivot && Data[++Partition1] <= Data[Pivot] )
				;
			while ( Partition2 > Pivot && Data[--Partition2] > Data[Pivot] )
				;
			while ( Partition1 != Pivot || Partition2 != Pivot )
			{
				swap ( Data [Partition1], Data [Partition2] );				
				if ( Partition1 == Pivot )
				{
					Pivot = Partition2;
					while ( Partition1 < Pivot &&
							     Data[++Partition1] <= Data[Pivot] )
						;
				}
				else if ( Partition2 == Pivot )
				{
					Pivot = Partition1;
					while ( Partition2 > Pivot &&
							     Data[--Partition2] > Data[Pivot] )
						;
				}
				else
				{
					while ( Partition1 < Pivot &&
							     Data[++Partition1] <= Data[Pivot] )
						;
					while ( Partition2 > Pivot &&
							     Data[--Partition2] > Data[Pivot] )
						;
				}
			}
			QuickSort ( Data, Pivot );
			QuickSort ( &Data[Pivot+1], Size - 1 - Pivot );
		}
	}
}
In doing that I got a bunch of compile error that appeared to be related to the conditional operators '<', '>', '<=', '>=', etc. Do I need to go and modify the original Name class to overload the operators?
__________________
PEBKAC = Problem Exists Between Keyboard And Chair

Reply With Quote
  #2  
Old March 23rd, 2005, 10:37 AM
InfoGeek InfoGeek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: In front of my Computer
Posts: 40 InfoGeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 12 m 33 sec
Reputation Power: 9
Quote:
template <class Type>
void Swap ( Type &First, Type &Second )
{
int Temp = First;
First = Second;
Second = Temp;
}


why int? it must be Type.

Reply With Quote
  #3  
Old March 23rd, 2005, 10:42 AM
InfoGeek InfoGeek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: In front of my Computer
Posts: 40 InfoGeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 12 m 33 sec
Reputation Power: 9
Quote:
Do I need to go and modify the original Name class to overload the operators?

Well yes, the operators you use in the quicksort template must be defined for the types you're passing to that template function.

Reply With Quote
  #4  
Old March 23rd, 2005, 11:18 AM
Voelcker's Avatar
Voelcker Voelcker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Maryland
Posts: 225 Voelcker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 51 m 59 sec
Reputation Power: 9
How careless of me, I missed that, I'll change the 'int' in the Swap() function to 'Type'.

Thanks for your help... I'll give it a shot tonight and post back if necesary.

Reply With Quote
  #5  
Old March 23rd, 2005, 08:57 PM
Voelcker's Avatar
Voelcker Voelcker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Maryland
Posts: 225 Voelcker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 51 m 59 sec
Reputation Power: 9
I used the same logic but a simpler QuickSort function, everything working like a charm! Thanks. (Oh, I did overload the additional operators.)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Quicksort array of class objects

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap