排序向量问题 [英] Sorting vectors problem

查看:83
本文介绍了排序向量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在java中有这个排序的向量,除了我实现SetSorted()之外,大多数事情似乎都有用,算法应该按字母顺序对输入进行排序,例如我在AddItem()中放入一些值但不幸的是,我得到的结果是NULL,

例如,如果实现了这样简单的事情:

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 :

SortedVector sv = new SortedVector();
   sv.AddItem("a");
   sv.AddItem("c");
   sv.AddItem("v");
   sv.AddItem("b");
   sv.AddItem("e");



我认为代码几乎是正确的但我可能只是没有看到那个小问题在哪里(问题可能只是在SetSorted中( )方法)。

任何人都可以帮我找到问题并帮我解决问题,我一直坚持这个问题。



谢谢



这是代码:


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 :

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

推荐答案

我认为以下两种方法和构造函数需要进行更改:



1.而不是像200K那样定义大尺寸,在构造函数中定义小尺寸10。大尺寸可能会造成性能问题。



2.在SetSorted()方法中,为什么需要这个if(data.length< = 1)条件。请删除此条件。



3.在AddItem()方法中,一旦达到最大限制,您需要创建一个Temp String数组并分配旧的String值它,并创建一个具有新限制的数据字符串数组,并获取分配回数据数组的旧值,例如:





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:


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





并且还从AddItem()方法中删除了SetSorted()。



我觉得不是使用String而是可以使用StringBuffer。



问候,

Panduranga。



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

I feel instead of using String you can use StringBuffer.

Regards,
Panduranga.


这篇关于排序向量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆