I found this code on the net, but it doesn't work.
Can someone provide me with some guidance.
i created an accending sorted array, then executed the following code. Is this the most efficient way to do this? Is this a valid way to do this?
Above code found from this linkCode:// Assuming array ranges from [0..arraySize-1] GetFromOrderedArray(0,arraySize-1) . . void GetFromOrderedArray(int lowBound,int highBound) { if (highBound < lowBound) return; middlePos = lowBound+(highBound-lowBound)/2 // middlePos is now at the element in the middle // between lowBound and highBound, so we just add // it to the tree AddElement ( theOrderedArray[middlePos] ) // Pick the middle one "to the left" AddFromOrderedArray(lowBound,middlePos-1) // Pick the middle one "to the right" AddFromOrderedArray(middlePos+1,highBound) }
I inserted test data to trace (no compiler available).
An array
- 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - <-index
--------------------------------
|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |9 | <-Data
--------------------------------
In my trace it inserts 5,2,1,7,8,9 in that order.
On my compile earlier at home it only inserted 2.
more trace details
recursive calls |Hi|Low| middlePos <-column labels
start|8|0|4|
rc1|3|0|1
rc2|1|0|0
nrc1|8|5|6
nrc2|8|7|7
nrc3|8|8|8
nrc4|8|9|