Problem in implementing Sorted List by Array in Java

425 Views Asked by At

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..

3

There are 3 best solutions below

0
On BEST ANSWER

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); } }

}

4
On

Your insertion code doesn't work.

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

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

2
On

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;
}

}