• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

programming question

trek

Senior member
How do you go about sorting 4 words in alphabetical order without using arrays (or anything else fancy), cause i haven't 'learned' those yet....

Anyway I try and do it becomes impossibly long...
 
if you can't use arrays you can still use 8 separate string variables (raw x 4, sorted x 4), or 4 +1 for swapping in place.

(ed) for n=4 a bubble sort is actually a good choice, though a bad habit to pick up 🙂
 
you can use the xor function to swap two binary numbers 😉

for example, 1001 and 1100

A = 1001
B = 1100

A = A xor B = 1001 xor 1100 = 0101
B = A xor B = 0101 xor 1100 = 1001
A = A xor B = 0101 xor 1001 = 1100

A = 1100
B = 1001
 
Originally posted by: her209
you can use the xor function to swap two binary bumbers 😉

for example, 1001 and 1100

A = 1001
B = 1100

A = A xor B = 1001 xor 1100 = 0101
B = A xor B = 0101 xor 1100 = 1001
A = A xor B = 0101 xor 1001 = 1100

A = 1100
B = 1100

a^b :: b^a :: a^b swaps in place

and wtf does this have to do with his question
 
Originally posted by: Ameesh
why dont you use an array they are not that fancy.
only allowed to use what I have learned in class...

Originally posted by: notfred
in perl, you can do this:
$var1, $var2, $var3, $var4 = sort($var1, $var2, $var3, $var4);
thats cheating, and this is turbo pascal 😛
 
Have you learned while and if yet? i.e. in c++

while ( not sorted )
{
make it a little more sorted
}

or

do {
make it a little more sorted
} while ( not sorted )

given that big of a hint it should be fairly easy, though some "helpful" idiot will probably show their l33tness by posting a full solution and ruining your chance to figure it out yourself.
 
Originally posted by: trek8900
im all for someone "helpful"! I've spent too much time on it already
it's an easy problem, if you don't learn to solve the easy problems now you'll sink deeper and deeper as the class goes on. Future classes will also be harder than they should be, your GPA will suffer, you won't be able to get a job, and you'll end up begging for booze money on a street corner.

Your entire future depends on you solving this problem yourself.
 
Hmmm.... if you've spent so much time, and still can't get it, maybe it means you don't deserve to get the answer here.
Sorry, but its the honest truth. I've always thought giving the answer on these forums was the dumbest thing ever. Theres no much point to grades when everythings posted. The point is to test and see how much you know, understand and can apply...
 
More seriously, one way to solve a "hard" problem is to try solving a simpler case of it first.

If the assignment were to sort just 2 strings, how would you do it without arrays?

Then consider 3 strings.
 
but for 2 you only need 1 if statement, correct?

now, do you understand how bubble sort works not just how to code it? have you made a little list on paper, with (say) 4 items and done some iterations to see how the numbers change?

bubble sort normally decreases the loop count by 1 each iteration, but it still works fine if you always loop 1..N as long as you figure out when to stop. Try this on paper too.

Or just turn in the billion if statement version I guess, but you'll be passing up a chance to work on your problem-solving, which you'll really need if you ever want (or need) to do any programming outside of classes.
 
yep, that should work fine, though I like my way better -- only 4 "if" and 1 "while" 🙂

yours is the next-best way to do it, and reducing the unsorted set size by one each time was a good approach (done any recursion before?)
 
ya, in java, but thats ahead of this class too

edit: im still not seeing how you can use a loop 🙁

edit again: thanks for helping 🙂
 
you're welcome 🙂

I wouldn't mind seeing your final version to compare against mine, you can post another link or email a copy (see my profile).
 
Back
Top