Sorting mod

Pages: 12
MIKEY!
DS Malik maybe full of Sh*T

I posted this question in beginners someone had this to reply


1
2
3
4
5
You need to either define operator< for your class or provide a comparison function and then call std::sort.

"Neither arithmetic nor relations operations are allowed on struct(s)"

That is incorrect.


Curious huh
closed account (D80DSL3A)
It would be necessary to write an overload for operator < if you were going to use the std::sort() to sort your array of structures.

However, you have written your own sort routine, custom for sorting infoType objects. You may define what < means there.
Eg;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (i = 0; i < size - 1; i++)
	{
		smallest = i;
 
		for (j = i + 1; j < size; j++)
		{
                        // this condition defines what < means to infoTypes
			if (tele[j].lname <  tele[smallest].lname )// compare last names
			{
				infoType temp = tele[j];// here's the swap
                                tele[j] = tele[smallest];
                                tele[smallest] = temp;
			}
		}
 
	//	tele[i].swap(tele[smallest]); <-- you were not far off here
	}

You got close with that swap() call! It's in the wrong place though.
If you #include<algorithm> then that would be
swap( tele[j], tele[smallest] );

You have things very confused here. Hope this helps out.
Last edited on
fun2code (936)

I'm very confused.
Can you please clear this up for me.

I have used your algorithm. I did not get any errors. But I have not put it to use yet because I taking 5 minutes from something else to see what the info was on here.
Anyways
How does this define < ? Can you explain.
1
2
// this condition defines what < means to infoTypes
			if (tele[j].lname <  tele[smallest].lname )// compare last names 


tele[i].swap(tele[smallest]); what the heck is this? This was an attempt to swap the 2 I thought this was a function. My lab aid used this for a sorting just strings so I thought that I could modify it ...
The confusion is that I read in my book that ....

"Neither arithmetic nor relations operations are allowed on struct(s)"
So I started falling down the rabbit hole. I thought that is why I was getting some of the errors I posted earlier. Because I couldn't do != and == on infoType.

Really a lot of the spiral into the confusion was because of this and I'm not so good at programming haha. But can you please explain this to me. I don't have any grasp on my question I posted above regarding <

THANKS! and is the error not all control paths return a value something to worry about ? ha

closed account (D80DSL3A)
jlillie wrote:
How does this define < ? Can you explain.


I was hoping that the expression if( name1 < name2 ) would make sense.

name1 is less than name2 if name1 comes before name2 alphabetically.

Your sort routine will work fine.
YOU DO NOT HAVE TO DEFINE ANY OPERATORS OR OTHER FUNCTIONS

Please, just post your working sort function for strings.
I will convert it to work for infoType objects for you OK?
Last edited on
The one you suggested works fine. I will give you the other one. But you have to tell me why I don't need any of that crap. Like I said my text said I couldn't use the things. I know the alphabet ha. But why on earth did everyone tell me all this mumbo jumbo? and you say "ahh no its fine" You have to explain please I won't sleep tonight.

This is the other function. My lab aid got it off google I suppose. Since my teacher says we do not have to reinvent the wheel.
YOU DO NOT HAVE TO DEFINE ANY OPERATORS OR OTHER FUNCTIONS
WHY!!!...no but seriously please explain...this way I can actually take something from this mess.Thansk


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void selectionSort(string words[], int size)
{
	int i, j;
	int smallest;
	string Word1 = "", Word2 = "";
 
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;
 
		for (j = i + 1; j < size; j++)
		{
			if (words[j] != "" && words[smallest] != "")
			{
				Word1 = words[j];
				Word2 = words[smallest];
				Word1[0] = tolower(Word1.at(0)); // Temp variables for accurate sorting.
				Word2[0] = tolower(Word2.at(0));
				if (Word1 < Word2)
				{
					smallest = j;
				}
			}
		}
 
		words[i].swap(words[smallest]);
	}
}
closed account (D80DSL3A)
I'm glad you posted the original one.

I'm not sure how to explain to you.
You can put whatever condition you like on line 19 in your code above.

This existing string sorting function works because of line 19.

Try out this function for your infoType array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void selectionSort(infoType tele[], int size)
{
	int i, j;
	int smallest;
//	string Word1 = "", Word2 = "";
 
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;
 
		for (j = i + 1; j < size; j++)
		{
			if (tele[j].lname != "" && tele[smallest].lname != "")
			{
				
			//	if (Word1 < Word2)// old rule
                                if( tele[j].lname <  tele[smallest].lname )// new rule
				{
					smallest = j;
				}
			}
		}
 
		infoType temp = tele[j];// here's the swap
                tele[j] = tele[smallest];
                tele[smallest] = temp;
	}
}
But you didn't explain ... how come even my book said the the realational operators dont' work for structs and then you say it does? Why is that ...Thx

Come on dude you got to explain please. Driving me nuts
closed account (D80DSL3A)
Does the new function work?

I have already explained! Line 17 in the new function above is where you make the rule for which object is less than the other.

You must be reading/hearing stuff which applies if you are using some generic sort function (such as std::sort).

But you aren't. You are using your own, where you can set the rule yourself.

I cannot explain it any more clearly than that.
When you reply, please answer:

Does the new sort function work or not?

EDIT: I tested the new function and it does not work.
I am not going to troubleshoot your sort function for you.

Post back with a known to be working sort function and I will try again.

EDIT2: It is definitely the selection_sort() you gave.
I adapted a bubbleSort() I have and it worked fine.
Last edited on
But you didn't explain ... how come even my book said the the realational operators dont' work for structs and then you say it does? Why is that ...Thx


I imagine what your book said is that the relational operators are not overloaded for types you create by default. You can, however, create your own if you desire to. On the other hand, the overloaded operators for types like std::string don't stop working if you put objects of those types into a struct. You can still compare the members directly without defining overloaded operators for the struct/class.
fun2code (939)


1
2
3
4
5
6
7
8
9
Does the new sort function work or not?

EDIT: I tested the new function and it does not work.
I am not going to troubleshoot your sort function for you.

Post back with a known to be working sort function and I will try again.

EDIT2: It is definitely the selection_sort() you gave.
I adapted a bubbleSort() I have and it worked fine.


All I did was put it in my program It's just like hanging out because I haven't called my sort function from the function I want to use. I have been doing other homework sorry. I don't understand this ..


1
2
3
4
5
6
7
EDIT: I tested the new function and it does not work.
I am not going to troubleshoot your sort function for you.

Post back with a known to be working sort function and I will try again.

EDIT2: It is definitely the selection_sort() you gave.
I adapted a bubbleSort() I have and it worked fine.

Are you talking about the function that you just gave me doesn't work so you have created a new one? The function that I gave you was the one I was trying to modify that function is correct as far as I know. The one I gave you for strings. I feel slow I don't know what your telling me here. I have this section to finish then I will work on programming more. Thanks more helping me.

And thank you cire.
So the operations that are defined for string are not void all of a sudden because they are within a struct. The same as all the other types like int and char...? Thank you thank you!
Fun2code I used this one to sort alphabetical order from a file before


The other one I gave was a working function too. Here is this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Function to sort array of strings
void selectionSort(string list[], int size)
{
	int i = 0, j = 0, smallest = 0; 
    string temp = "" ; string str1 = ""; string str2= "";
	
	for(i = 0; i < size -1; i++)
	{
		smallest = i;
	
	for(j = i + 1; j < size; j++)
	{  
		if (list[j] != "" && list[smallest] != "")
		{
			str1 = list[j];
			str2 = list[smallest];
			str1[0] = tolower(str1.at(0));
			str2[0] = tolower(str2.at(0));
		
		
			if( str1 < str2)
			{
				smallest = j;
			}
		}
	}
	    //swap
		temp = list[smallest];
		list[smallest] = list [i];
		list [i] = temp;
	}
}
closed account (D80DSL3A)
I'l try out that sort function on an array of strings.
If it works then I'll convert it for infoType and test that...
OK Thanks Schrödinger:)

It should work the other one should of worked for the array of strings too.



closed account (D80DSL3A)
Your sort functions were fine.
I goofed. I made a note where.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void selectionSort(infoType tele[], int size)
{
	int i, j;
	int smallest;
	
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;

		for (j = i + 1; j < size; j++)
		{
			if (tele[j].lname != "" && tele[smallest].lname != "")
			{

			//	if (Word1 < Word2)// old rule
                if( tele[j].lname <  tele[smallest].lname )// new rule
				{
					smallest = j;
					cout << "smallest = " << smallest << endl;
				}
			}
		}

		infoType temp = tele[smallest];// here's the swap
        tele[smallest] = tele[i];// in last version j was here
        tele[i] = temp;//              instead of i
	}
}
I see I see. I was thinking I can't see why it was wrong because I used the one I just gave in my last homework. I'm almost done with this other homework then I will program for a while. Thank you for your help.
The sort function appears to be acting up. I entered a last name of apple and it was at the end of the list. It should of been first.
Strange times.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Sort Function
 void sort(infoType tele[], const int& size)
 {
	
	int i, j;
	int smallest;
	
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;

		for (j = i + 1; j < size; j++)
		{
			if (tele[j].lname != "" && tele[smallest].lname != "")
			{
				 if( tele[j].lname <  tele[smallest].lname )
				{
					smallest = j;
				}
			}
		}
			//swap
			infoType temp = tele[smallest];
			tele[smallest] = tele[i]; 
			tele[i] = temp;              
	}
}

don't see why yet
never-mind I need case sensitive comparison.
closed account (D80DSL3A)
You're welcome. Did you notice cires post above?
As he pointed out we are not applying any relational operator to an infoType here.

We are applying the comparison operator < in just one place, on line 16 above where it is applied between 2 strings. We are not comparing whole infoTypes here.
Here's a comparison:
1
2
3
if( tele[i] < tele[j] )  // here two infoItems are being compared. We can't do this.

if( tele[i].lname < tele[j].lname )  // here two strings are being compared. No problem. 

I hope that helps to clear your confusion a bit.
Yah it really does. Dude I'm almost done with my program I stayed up late and it was working. Now I'm having an issue when inside my program. When I'm done with a function it is supposed to return to the menu. But all that happens is the program stops. Here is the link
http://www.cplusplus.com/forum/general/85806/#msg460498

I just need to format and figure out why it isn't continuing to work. When it was working.
Topic archived. No new replies allowed.
Pages: 12