Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello , I have this sorted vector in java , most of the things does seem to function except when I implement the SetSorted() , the algorithm should sort the inputs alphabetically when I for example put in some values in the AddItem() , however the result I am unfortunately getting is NULL ,
for example if something simple like this is implemented :
Java
SortedVector sv = new SortedVector();
   sv.AddItem("a");
   sv.AddItem("c");
   sv.AddItem("v");
   sv.AddItem("b");
   sv.AddItem("e");

I think the code is almost right but I am probably just not seeing where that slight problem is ( , the problem probably should just be in the SetSorted() method ) .
Can anybody please help me find that problem and help me fix it , I am really stuck with this problem for some time .

Thank you

Here is the code :
Java
package ads2;

public class SortedVector {

    private int length;
    private int maximum;
    private int growby;
    private int temp;
    private int x = 0;
    private int high;
    private int middle;
    private int low;
    private String  temp1;

//Keeps track of the smallest string's index
int  shortestStringIndex;

    private String[] data;

    public SortedVector()
    {
        length = 0;

        maximum = 200000;

        data = new String[maximum];
    }

   public void AddItem(String value)
    {
        if (length == maximum)
        {
        maximum += 200000;
        }

        // data[length] = value;
           data[10] = value;
        length++;

        SetSorted();
    }

    public void SetSorted() {

    if(data.length <= 1) {
        for(int i = data.length-1; i >= 0; i--) {
            for(int j = 0; j < i; j++) {

                if(data[j].compareTo(data[j + 1]) > -1) {
                    String tmp = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = tmp;
                }
            }
        }
       }

    // for(int i = 0; i < data.length; i++) {
    // System.out.print(data[i] +" ");
}

    public void SetGrowBy(int growby)
    {
       maximum += growby;
    }

    public int GetCapacity()
    {
        return maximum;
    }

    public int GetNoOfItems()
    {
        return length;
    }

    public String GetItemByIndex(int index)
    {
        return data[index];
    }

 public int FindItem(String search)
  {

    for (x=0;x<=length; )
         {
             middle =((low + high)/2);
            if (data[middle].compareTo(search)==0)
            {
                return middle;
            }
            else if (data[middle].compareTo(search)<0)
            {
               low = middle;
               x++;
               return FindItem(search);
            }
            else
            {
               high = middle;
               x++;
               return FindItem(search);
            }
   }
   return -1;
  }

    public boolean Exists(String search)
    {
        boolean output;

        int y;
        y = 0;

        while (data[y] != search && (length - 1) > y)
        {
            ++y;
        }

        if (data[y] == search)
        {
            output = true;
        } else
        {
            output = false;
        }

        y = 0;

      return output;
    }

    public void InsertItem(int index, String value)
    {
        if (length == maximum)
        {
        maximum += 200000;
        }

        for(int i = length - 1; i >= index; --i)
        {
            data[i + 1] = data[i];
        }

        data[index] = value;

        length++;
    }


    public void DeleteItem(int index)
    {
       for(int x = index; x < length - 2; ++x)
       {
            data[x] = data[x + 1];
        }

       length--;
    }

    public String toString()
    {
        String res = "";

        for (int i=0; i<length; i++)
            res+=data[i] +  "; ";

        return res;
    }
}
Posted
Updated 26-Jan-15 11:59am
v2
Comments
Sergey Alexandrovich Kryukov 26-Jan-15 22:53pm    
Slight problems? Look what you are doing: hard-coded maximum = 200000. If you mean to use the biggest value, use int.MaxValue. (For floating point, use -Inf or +Inf.) It won't probably solve your problem, but how can you hope to get something good without being a least minimally accurate. How can you use names like "i", use ""? (Use string.Empty...) And so on...
—SA

1 solution

I think changes required in below two methods and constructor:

1. Instead of define big size like 200K define small size say 10 in constructor.Big size may create performance problem.

2. In SetSorted() method, why this "if(data.length <= 1)" condition is required. Please remove this condition.

3. In AddItem() method , Once the max limit is reached, you need to create a Temp String array and assign the old String values to it, and create a data String array with new limit, and get the old values assigned back to data array, for example:


C#
if (length == maximum)
        {
            String[] tempData = new String[length];
            tempData = data;
            maximum += 10;
            //System.out.println("The Maximum : " + maximum);
            data = new String[maximum];
            for (int i=0; i<tempData.length; i++)
                data[i] = tempData[i];

        tempData = null;
        }


And also removed SetSorted() from AddItem() method.

I feel instead of using String you can use StringBuffer.

Regards,
Panduranga.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900