0

There seems to be a problem in add method of the class I have written.. I want to make a SortedList using an array, but I can't figure out what the problem is. This is my code:

public class SortedList {

    private Integer[] elements;
    private int size;
    private int capacity;

    public SortedList(int cap) {

        elements = new Integer[cap];

        if (cap > 0)
        {
            cap = capacity;
        }
        else
            capacity = 10;

    }

    public boolean isEmpty()
    {
        return size == 0;
    }

    public boolean isFull()
    {
        return size == capacity;
    }

    public int size()
    {
        return size;
    }

    public void doubleCapacity()
    {
        capacity = capacity * 2;
    }

    public void add(Integer el)
    {
        if(this.isEmpty())
        {
            elements[0] = el;
            size++;
        }

        else if(this.isFull())
        {
            this.doubleCapacity();
            for(int i = 0; i<this.size(); i++)
            {
                if(el >= elements[i])
                {
                    elements[i+2] = elements[i+1];
                    elements[i+1] = el;
                }

                else
                {
                    elements[i+1] = elements[i];
                    elements[i] = el;
                }
            }
            size++;
        }
        else
        {
            for(int i = 0; i<this.size(); i++)
            {
                if(el >= elements[i])
                {
                    elements[i+2] = elements[i+1];
                    elements[i+1] = el;
                }
                else
                {
                    elements[i+1] = elements[i];
                    elements[i] = el;
                }
            }
            size++;
        }

    }

    public String toString()
    {
        String s = "";
        s = s + "<SortedList[";
        for(int i = 0; i < this.size(); i++)
        {
            s = s + elements[i];
            if(i < this.size()-1)
                s = s + ",";
        }
        s = s + "]>";
        return s;
    }


    public static void main(String[] args)
    {
        SortedList sl = new SortedList(5);
        sl.add(3);
        //sl.add(2);
        sl.add(4);
        sl.add(5);
//      sl.add(6);
        System.out.println(sl.toString());
    }



}

My code works if I only add 2 Integers to my list, but when I try to add the numbers 3,4,5 then I get 3,5,5...

What can be the problem? Thanks..

1
  • It is howework, but I don't have to do it. I just want to learn how to do this with an array!
    – Loolooii
    Feb 8, 2011 at 21:49

3 Answers 3

1

public class SortedList {

private Integer[] elements;
private int size=0;
private int capacity;

public SortedList(int cap) {

    elements = new Integer[cap];

    if (cap > 0)
    {
        capacity = cap;
    }
    else
        capacity = 10;

}

public boolean isEmpty()
{
    return size == 0;
}

public boolean isFull()
{
    return size == capacity;
}

public int size()
{
    return size;
}

public void doubleCapacity()
{
    capacity = capacity * 2;
}

public void add(Integer el) throws Exception{
    elements[size] = el;
    size++;
    if(size>capacity){
        throw new Exception("Size Exceeded");
    }
}

public String toString()
{
    sort();
    String s = "";
    s = s + "<SortedList[";
    for(int i = 0; i < this.size(); i++)
    {
        s = s + elements[i];
        if(i < this.size()-1)
            s = s + ",";
    }
    s = s + "]>";
    return s;
}

public void sort(){
    for (int i=0; i <size()-1; i++) {
        if (elements[i] > elements[i+1]) {
            // exchange elements
            int temp = elements[i];
            elements[i] = elements[i+1];
            elements[i+1] = temp;
        }
    }
}

public static void main(String[] args)
{
    try {
        SortedList sl = new SortedList(5);
        sl.add(3);
        //sl.add(2);
        sl.add(6);
        sl.add(5);

// sl.add(6); System.out.println(sl.toString()); } catch (Exception ex) { Logger.getLogger(SortedList.class.getName()).log(Level.SEVERE, null, ex); } }

}

0
1

Your insertion code doesn't work.

elements[i+1] = elements[i];
elements[i] = el;

What happens to the old value of elements[i+1]?

4
  • I don't know.. I have to store it in a variable?
    – Loolooii
    Feb 8, 2011 at 21:33
  • @Nima: Where should elements[i+1] end up after you've finished inserting?
    – Anon.
    Feb 8, 2011 at 21:41
  • After the integer I want to insert?
    – Loolooii
    Feb 8, 2011 at 21:56
  • @Nima: And then where should elements[i+2] end up?
    – Anon.
    Feb 8, 2011 at 23:05
1

I'd recommend the following changes to the previous solution. If you're only calling sort in toString(), your list is going to get out of order quickly in cases where you have multiple unsorted elements in a row (Now you could remove sort() from toString()). It's essentially a quick insertion sort that dies as soon as it can't make any more swaps down the list. Again, as dty suggested, a faster choice would be a binary search to find the insertion point.

public void doubleCapacity(){

capacity = capacity * 2;
Integer temp[] = new Integer[capacity];
for (int i = 0; i < size; i++){
    temp[i] = elements[i];
}
elements = temp;

}

public void add(Integer el){

if(size+1>capacity){
    doubleCapacity();
}
elements[size] = el;
size++;
sort();
}

public void sort(){

//Iterates down the list until it's sorted.
for (int i=size()-2; i >= 0 && (elements[i] < elements[i+1]); i--) {
        // exchange elements
        int temp = elements[i];
        elements[i] = elements[i+1];
        elements[i+1] = temp;
}

}

2
  • Forget that. If you're assuming the list is already sorted (which you must, by definition) then just use a binary search to find the insertion point.
    – dty
    Feb 8, 2011 at 21:35
  • Ah, good point dty. Your suggestion would definitely run faster, I forgot about that. If you can use the Arrays framework, then you could quite easily shift your values accordingly.
    – Cooper
    Feb 8, 2011 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.