trouble understanding this sort function

Hi ,

So I have been studying C++ for about a month now, and do not fully understand how this sort function is working. I



void sort(int*a,int*b){
for(int*c=b,t;c>a;)
if(t=*c--,*c>t)c[1]=*c,*c=t,c=b;
}


ok, why is the condition of the for loop (c>a), whose values are actual memory addresses? When I run the program and print out the values of a and c, they appear to stay the same, but the loop eventually terminates (so I assume that a change in these values causes the for-loop to end).

I understand the order of operators, and understand that commas are last.

With the if statement, first it decrements *c then assigns that value to t, then it is evaluating whether *c>t. If true then c[1] is assigned the value of *c, *c is then given the value of t, and c is given the value of b.

I also do not understand why c[1] is used.

I ran this program and the function does work;

#include <iostream>
using namespace std;


void sort(int*a,int*b){
for(int*c=b,t;c>a;){
if(t=*c--,*c>t){

c[1]=*c,*c=t,c=b;

cout << "at this point in function *a=" << *a << " and a=" << a << '\n';
cout << "at this point in function *b=" << *b << " and b=" << b << '\n';
cout << "at this point in function *c=" << *c << " and c=" << c << '\n';
cout << "at this point in function t=" << t << '\n';
cout << '\n';

system("PAUSE");
}

}
}


int main() {

int data[]={12,0,4,6,-2,-4};

int x=6;
int y=0;

cout << "starting array is={12,0,4,6,-2,-4}" <<'\n';

sort(&data[y],&data[(x-y)-1]);


cout << "the new array arranged is now=" ;

for(int j=0; j<x; j++) {

cout << data[j] << " ";
}
cout << '\n'<<'\n';


system("PAUSE");
return 0;
}




Notice I print out the values as they change within the function.

Yes, it is a bit of a messy program, but it does seem to work and I fully want to understand the process 'under the hood' of the function.

Any help would be appreciated, thanks!

Well I don't understand *any* of that code because it has to be put in code tags first.
sorry, here you go;



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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <iostream>
using namespace std;


void sort(int*a,int*b){
	for(int*c=b,t;c>a;){
		if(t=*c--,*c>t){
			
		c[1]=*c,*c=t,c=b;
		
		cout << "at this point in function *a=" << *a << " and a=" << a << '\n';
		cout << "at this point in function *b=" << *b << " and b=" << b << '\n';
		cout << "at this point in function *c=" << *c << " and c=" << c << '\n';
		cout << "at this point in function t=" << t << '\n';
		cout << '\n';

		system("PAUSE");
		}

	}
}


int main() {

	int data[]={12,0,4,6,-2,-4};

	int x=6;
	int y=0;

	cout << "starting array is={12,0,4,6,-2,-4}" <<'\n';
	
	sort(&data[y],&data[(x-y)-1]);


	cout << "the new array arranged is now=" ;

	for(int j=0; j<x; j++) {

		cout << data[j] << " ";
	}
	cout << '\n'<<'\n';

	
	system("PAUSE");
	return 0;
}



I think this is a lesson in how not to write code.
'I think this is a lesson in how not to write code.'


did I present this as perfect?

I said it was messy, and I just wanted to understand how that sort function was working.

It does work.

This is the beginners forum, what do you expect?

of course you were writing perfect code from the beginning, but an idiot like me does not have your deity-like intelligence.
I started to write an explanation but the phone rang - so it will have to wait.

It's a variation on the bubble sort?

I also do not understand why c[1] is used.
For any given pointer ptr, then ptr[0] is the value being pointed at, and ptr[1] is the next value along, and so on.
Sorry -- you did not present this as code you wrote but as code you were trying to understand. No offense intended.
Sorry -- you did not present this as code you wrote but as code you were trying to understand. No offense intended.


no problem,, I found that sort function on Reddit, and wrote a messy program around it in an attempt to understand how it was working.

I also do not understand why c[1] is used.
For any given pointer ptr, then ptr[0] is the value being pointed at, and ptr[1] is the next value along, and so on.


ok, thanks.Now I understand that part. I have seen the 'bubble sort' before.


What still is throwing me off is the condition of the for loop ;


1
2
3

for(int*c=b,t;c>a;)


as the function runs and prints values of the variables and pointers, the values of a and c seem to stay equal to their initial memory addresses. What then happens that causes the loop to end is a mystery to me..
Pointer a and b never changes - it is pointer c that does the work.
The values to be sorted are stored in an array/buffer.
pointer a is set to the start of the buffer and b to the last value in the buffer.


for(int*c=b,t;c>a;)
The loop ends when pointer c reaches pointer a - the only way for that to happen is when the
all the values have been sorted.

1
2
if(t=*c--,*c>t)
c[1]=*c,*c=t,c=b;

This takes the current value pointed at by c (stores it in t), moves c back by one, and compares the
current value with the previous one. If the current value is greater , the values are swapped, and pointer c is reset (c =b). If there is no need to swap, then pointer c jis not reset

In other words- say the buffer have 5 values 1 2 5 4 3.
The function is called with pointer a pointing to the 1 (start of buffer) and pointer b pointing to the 3 (end of the buffer).
pointer c is made to point to the same as b so c points to 3.

The value 4 is compared to the value 3. 4 is greater so they are swapped.

The buffer now looks like this
1 2 5 3 4.

Because a swap was made pointer c is reset to the end of the buffer and the loop runs again.
3 and 4 are compared - NO SWAP - pointer c is not NOT reset
5 and 3 are compared - SWAP - Buffer now looks like this 1 2 3 5 4. pointer c is reset.
5 and 4 are compared - SWAP - Buffer now looks like 1 2 3 4 5 - pointer c is reset.
Now you will see that this time round the loop, they will NO SWAP, pointer c will not be reset, and pointer c will continue to be decremented, and will reach pointer a and the loop will terminate (because c > a will be false) - sort complete

Thanks guestgulkan, I appreciate the explanation.

I one thing I am not clear on is the order of operations within the if condition.


if(t=*c--,*c>t)
c[1]=*c,*c=t,c=b;


This takes the current value pointed at by c (stores it in t), moves c back by one, and compares the
current value with the previous one. If the current value is greater , the values are swapped, and pointer c is reset (c =b). If there is no need to swap, then pointer c jis not reset



within those brackets would the '--' decrement to *c happen first, then have that value be stored in t? you seem to be suggesting that the 't=*c' is happens first then *c is decremented.

Actually, no. He phrased it correctly. a=*b--; is the same as a=*b; b--;.
By the way, a=*--b; is the same as a=b[-1]; b--;.
I don't think it's such a good idea to cram so much stuff into a condition, though.
Last edited on
Topic archived. No new replies allowed.