Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / bubblesort problem

Author
Message
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 5th Mar 2012 02:08
Hello,

I'm having a problem doing a bubble sort.


The intent is in an 8x8 array to take the 0's in any particular column and bubble them to the top. This would not numerically sort any other value in the array.

So if you started with this:
2
0
3
1
0
2
4
5


You should end with this:

0
0
2
3
1
2
4
5

But instead you actually get this:

0
0
0
0
0
2
4
5

This code is what I have at the moment:


What I think is happening is the operation to copy the 0 up always happens, but the operation to copy the value above downwards is either not working or getting over written. I cant figure out which and any adjustments I make either result in no bubble sort at all or worse behavior than what is shown here

Thanks for any help in advance!
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 5th Mar 2012 04:11
You state the values as if the GameBoardBubble() array is singularly dimensioned, but your code shows it being multi-dimensioned. Which is it? If your code isn't too long, maybe you could post the rest of it.

So many games to code.......so little time.

erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 5th Mar 2012 04:21
LBFN:

1) You are correct my array is multi dimensional; the example shows the expectations along a single column to avoid confusion for the explanation.

2)Its over 1000 lines right now so that's probably impractical. Last question I posted with about 500 lines never got an answer so I clipped out the portion that was brief enough to be a succinct question.

3)If it really needs to be done I can create an example with a single dimension ; its really the same problem every column so its the instructions in the X part of the for statement that have to be causing it is my current guess:



thanks!
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 5th Mar 2012 05:01 Edited at: 5th Mar 2012 05:14
Here, try this:



EDIT: Here it is with both dimensions:



So many games to code.......so little time.

erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 5th Mar 2012 05:35
LBFN ; very interesting .. its about bedtime here, I get up at 4:30am for work so I rarely stay up very late .

I see your suggestion works just fine; I'll have to try to integrate it into my program tomorrow.

Thank you for the suggestions/tips!
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 6th Mar 2012 05:05
So I had a chance to integrate this into my program

What ended up working was the following



Honestly I have no idea why this one worked and my previous examples above did not ... but thank you for the help your example seemed to help it click together for me!
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 6th Mar 2012 06:06
Notice the extra for - next loop (z) that was not in your original code. You needed to run it through additional iterations in order to get the 0s to move up.

You're welcome - glad to help and good luck.

So many games to code.......so little time.

Login to post a reply

Server time is: 2024-11-23 20:53:27
Your offset time is: 2024-11-23 20:53:27