GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Oct-2008, 02:36
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Finite Set Question Related to Counting Sort


Hello to all expect C++ programmer,
I have coded a CS but has some memory problem because i don't know how to ensure the the set is finite and memory allocated is enough.

For instance,
CPP / C++ / C Code:
int number [] = {8, 5, 6, 3, 7, 4, 1, 2};
This code works well but when i change it to
CPP / C++ / C Code:
int number [] = {8, 5, 6, 3, 7, 4, 2, 2};

I wonder what is finite set ?
How to i ensure the sortee is finite set ?

Below is my code :

CPP / C++ / C Code:
#ifndef COUNTINGSORT
#define COUNTINGSORT

namespace mystd
{
	int Min(int number[], int size)
	{
		int min = number[0];
		for (int loop=1;loop<size;++loop)
		{
			if (number[loop] < min)
			{
				min = number[loop];
			}
		}

		return min;
	}

	int Max(int number[], int size)
	{
		int max = number[0];
		for (int loop=1;loop<size;++loop)
		{
			if (number[loop] > max)
			{
				max = number[loop];
			}
		}

		return max;
	}


	template <typename T>
	void CountingSort(T *sortee, int);


	// Specialized version of CS
	template <>
	void CountingSort <int>(int number [], int size)
	{
		int minValue = number[0], maxValue = number[0];
		
		minValue = Min(number, size);
		maxValue = Max(number, size);

		int tempSize = maxValue - minValue + 1;
		int *countingTemp = new int[tempSize];

		for (int loop=0;loop<tempSize;++loop)
		{
			*(countingTemp + loop) = 0;
		}

		// Put count in CountingTemp
		for (int loop=0;loop<size;++loop)
		{
			++countingTemp[number[loop] - minValue];
		}

		tempSize = size - 1;
		int anotherLoop = size - 1;
		while(tempSize >= 0)
		{
			while(--(*(countingTemp + tempSize)) >= 0)
			{
				if (anotherLoop >= 0)
				{
					number[anotherLoop--] = tempSize + 1;
				}
			}
			--tempSize;	
		}

		delete [] countingTemp;
	}
}




#endif

  #2  
Old 04-Oct-2008, 11:45
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 845
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Finite Set Question Related to Counting Sort


By 'CS' do you mean character string? Shouldn't that be in type char?

> "i don't know how to ensure the the set is finite and memory allocated is enough"
If you declare an array it's about as finite as you can get.
The space allocated is only gauranteed to be large enough contain the specified number of items.
If you write outside of that space you WILL overwrite other data OR get a segfault.
Reading outside of that space can cause a segfault.
Not clear: do you want to have definite space defined? ...and are looking for a size value? like this?
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main(void)   /* note: the indice specifier in number2 is larger than specified elements */
{                  
  int number[]  = {8, 5, 6, 3, 7, 4, 1, 2, 0, 0 };  
  int number2[20] = {8, 5, 6, 3, 7, 4, 1, 2, 0, 0, 0, 0, 0}; 
  cout << "sizeof(number)= "<< sizeof(number) <<"    sizeof(number2)= "<< sizeof(number2) <<endl;
  return 0;
}
/* output:
sizeof(number)= 40    sizeof(number2)= 80
*/  
...and a little math will give you the number of items for counting. Is that any help?
Please don't use tabs in your code. It's in the guidelines and messes up when I paste it in my editor to try out and must fix....
  #3  
Old 04-Oct-2008, 22:24
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Finite Set Question Related to Counting Sort


Quote:
By 'CS' do you mean character string? Shouldn't that be in type char?

CS means counting sort.
Quote:
If you declare an array it's about as finite as you can get.
The space allocated is only gauranteed to be large enough contain the specified number of items.
If you write outside of that space you WILL overwrite other data OR get a segfault.
Reading outside of that space can cause a segfault.


I sure about that.

Quote:
Not clear: do you want to have definite space defined? ...and are looking for a size value? like this?

My question is What is finite set and how to ensure the data is finite before calling counting sort to ensure that memory is enough to contain the counting.

I know that Tabs is not standard over editor/OS. Sorry. I use MS VS C++ 7.0(2005);

Do you know any editor that same indentation and have code completion features with MS VS C++ ?

I try to switch to Linux.

Kdevelop need to adjust indentation when create C++ project.

Emacs is too difficult to use.

Thanks for your help.
  #4  
Old 05-Oct-2008, 00:24
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 845
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Finite Set Question Related to Counting Sort


> What is finite set and how to ensure the data is finite before calling counting sort to ensure that memory is enough to contain the counting.
Like said the array size is finite but the I guess the 'finite set' would be the number of array elements which have been assigned a value.
I guess you wouldn't want to sort 100 array elements if only 10 contained data.
Why no just pass the address of the array and it's size to the sort() and heve sort() handle all that.

> Sorry. I use MS VS C++ 7.0(2005);
...can't you tell it to use spaces instead of tabs? Complain to Bill about it! (and 'pause' too while you've got him on the phone)
Do you really need that monster? Code completion... bah! How are you supposed to learn with that going on?
I use gcc CLI and for an editor in windows I use 'textpad' in linux I use 'vim'. I never gave emacs much of a try...
  #5  
Old 07-Oct-2008, 02:48
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Finite Set Question Related to Counting Sort


Quote:
Like said the array size is finite but the I guess the 'finite set' would be the number of array elements which have been assigned a value.
I guess you wouldn't want to sort 100 array elements if only 10 contained data.
Why no just pass the address of the array and it's size to the sort() and heve sort() handle all that.

I think you have misunderstand me. Of course, we don' want sort 100 integer but only got 10 data.

The rules of finite set is to ensure memory allocated for counting array is same as the original array.

STL sort is good but not in linear time.

Quote:
..can't you tell it to use spaces instead of tabs? Complain to Bill about it! (and 'pause' too while you've got him on the phone)
Do you really need that monster? Code completion... bah! How are you supposed to learn with that going on?
I use gcc CLI and for an editor in windows I use 'textpad' in linux I use 'vim'. I never gave emacs much of a try...

This is the reasons i don't want switch to Linux. If linux program these two features, i have no longer be MS World because you can learn a lot of things from g++ compiler options.

Thanks.
  #6  
Old 07-Oct-2008, 06:11
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Finite Set Question Related to Counting Sort


Quote:
Originally Posted by Howard_L
> Sorry. I use MS VS C++ 7.0(2005);
...can't you tell it to use spaces instead of tabs? Complain to Bill about it! (and 'pause' too while you've got him on the phone)
Yes, you can. It's always been in there, under the editor options. The system( "pause" ) issue is more of a teaching issue than a Visual Studio issue. Yes, it arises from the fact that Visual Studio is not command line oriented when it comes to programming, but it's laziness that results in professors saying "Use this instead."
  #7  
Old 07-Oct-2008, 08:58
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 845
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Finite Set Question Related to Counting Sort


Quote:
I think you have misunderstand me.
yes , and I still don't really understand what you're after , you're talking over my head , sorry...
  #8  
Old 08-Oct-2008, 03:09
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Finite Set Question Related to Counting Sort


My question is

What is finite set and ensure the array is finite set before calling CountingSort ?

I not very good in Mathematics.

Thanks.
  #9  
Old 08-Oct-2008, 09:53
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 845
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Finite Set Question Related to Counting Sort


My understanding is a 'finite set' would be a closed set of elements. onlinemathlearning.com/finite-sets.html
That is it would have a starting point and an ending point and a total number of elements.
I believe your declared array above would fit that description. You know how to determine the number of elements.
I don't know how you could sort an infinite list or list of unknown length (unless there was a stated marker (like NULL)).
  #10  
Old 09-Oct-2008, 04:04
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Finite Set Question Related to Counting Sort


yes, i read that too but doesn't understand. Finally, i understand that finite set it is a set with accurate length.

This algorithm is suitable for lenght less than 100 due to extra memory space which is unpractical for commercial/Industry.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please, This is Difficult question try for me! bcompt143 C++ Forum 4 24-Aug-2008 09:14
Question related to Operator overloading Archer C++ Forum 6 09-Jun-2008 17:26
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
C++ Syntax Highlighter related question JdS GIDForums™ 4 25-Oct-2004 08:00
'Related articles' php /mysql question JdS MySQL / PHP Forum 4 06-Sep-2002 10:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.