would it be acceptible to use this in a bubble sort.?Code:a ^= b ^= a ^= b is the same as a ^= b b ^= a a ^= b which can be written as a = a xor b b = b xor a = b xor (a xor b) = a a = a xor b = (a xor b) xor a = b
would it be acceptible to use this in a bubble sort.?Code:a ^= b ^= a ^= b is the same as a ^= b b ^= a a ^= b which can be written as a = a xor b b = b xor a = b xor (a xor b) = a a = a xor b = (a xor b) xor a = b
Dangerous Dave
No
-Prelude
My best code is written with the delete key.
Why???
Dangerous Dave
Because it is undefined, Nutshell started a thread about this earlier and several reasons were given for why it shouldn't be used.
-Prelude
My best code is written with the delete key.
Here is a function you can use. I call it 'stringxor'. It is used for swapping strings:
Enjoy.Code:void stringxor( char *s1, char *s2 ) { int x,y= strlen(s1)>strlen(s2)?strlen(s1):strlen(s2); for(x=0;x<y;x++) s1[x]^=s2[x]^=s1[x]^=s2[x]; printf(":D"); }
Quzah.
What the hell happened to my sign in? Freeking cookies.
Quzah.
Hope is the first step on the road to disappointment.
I think i will stick with this for swapping the strings.Code:for(i=0; i <= SIZE - 1 ; i++) for(j =0; j <= SIZE - 1 ; j++) { while(strcmp(book_array[i].name, book_array[j].name) < 0) { strcpy(hold, book_array[j].name); strcpy(book_array[j].name, book_array[i].name); strcpy(book_array[i].name, hold); }
Dangerous Dave
> I think i will stick with this for swapping the strings.
That's a good idea, since mine was intended as a joke that will likely crash your application.
Quzah.
Hope is the first step on the road to disappointment.
Now quzah, you know better than to write code that doesn't self document. This is better:
-PreludeCode:char *stringxor( char *s1, char *s2 ) { int x,y= strlen(s1)>strlen(s2)?strlen(s1):strlen(s2); for(x=0;x<y;x++) s1[x]^=s2[x]^=s1[x]^=s2[x]; return "If you use this you are stupid :)"; }
My best code is written with the delete key.
1) Why are you using 'while'? It's just executing once. 'if' would be more obvious.Originally posted by Dangerous Dave
I think i will stick with this for swapping the strings.Code:for(i=0; i <= SIZE - 1 ; i++) for(j =0; j <= SIZE - 1 ; j++) { while(strcmp(book_array[i].name, book_array[j].name) < 0) { strcpy(hold, book_array[j].name); strcpy(book_array[j].name, book_array[i].name); strcpy(book_array[i].name, hold); }
2) To make that sorting even more optimized, start on 'i' instead of '0' in the second loop.
Code:for(i=0; i < SIZE - 1; i++) for(j=i; j < SIZE; j++) { if(strcmp(book_array[i].name, book_array[j].name) < 0) { strcpy(hold, book_array[j].name); strcpy(book_array[j].name, book_array[i].name); strcpy(book_array[i].name, hold); }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.