Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Binary Search Insert Function

Name: Anonymous 2009-03-17 10:36

I need to use binary search to insert an item into a sorted list.

Here is my code which is not sorting the items correctly.

template<class ItemType> void SortedList<ItemType>::InsertItem(ItemType x)
{
    int location = 0;
    int end = length-1;
    bool moreToSearch;

    moreToSearch = (location<length);

    while(moreToSearch)
        {
            int midPoint = (location + end) / 2;

            if(x>values[midPoint])
                {
                    location = midPoint + 1;
                    moreToSearch = (location<end);
                }   
            else if(x<values[midPoint])
                {
                    end = midPoint -1;
                    moreToSearch = (location<end);
                   
                }
            else
                {
                    moreToSearch = false;
                    location = midPoint;
                    break;
                }
        }

    for(int index=length;index>location;index--)
          values[index] = values[index-1];


    values[location]=x;
    length++;
}


What's wrong?

Name: Anonymous 2009-03-17 10:42

Hello homework!

Name: Anonymous 2009-03-17 10:44

Hello ugly indentation!

Name: Anonymous 2009-03-17 11:03

What's wrong?
You're using SEPPLES.

Name: Anonymous 2009-03-17 15:35

int midPoint = (location + end) / 2;
int midPoint = location + ((end - location) / 2);

Name: Anonymous 2009-03-17 15:54

>>5
Go go gadget sixth-grade math!

Name: Anonymous 2009-03-17 17:08

template<class ItemType>
void SortedList<ItemType>::InsertItem(const ItemType &x)

{
                int lol=0, hai=length;
                while (lol < hai)
                {
                        int midkips = lol+hai >> 1;
                        if (values[midkips] < x)
                                lol =++ midkips;
                        else
                                hai = midkips;
                }
                std::copy_backward(values+lol, values+length, values -~ length)[~0] = x;
                length -=- 1;
}

Name: Anonymous 2009-03-17 20:09

Just use the STL, that's what it's there for!

Unless you're not allowed to because you came to /prog/ with your FUCKING HOMEWORK, in which case you can go FUCKING DIE!

Name: Anonymous 2009-03-18 6:06

Resurrection bump

Name: Anonymous 2010-11-02 10:30

Name: Anonymous 2010-11-27 18:09

Don't change these.
Name: Email:
Entire Thread Thread List