排序矢量 - AddItem

时间:2016-12-14 23:28:59

标签: java algorithm vector data-structures

我写了一个完全有效的排序向量。但是我的Add方法很长,我觉得有很多冗余代码。

我写了一个二进制搜索函数,我想在我的Add方法中使用它,而不是在Add函数中进行比较。

以下是我的代码:

public class SortedVector
{

  private int  maxcap = 10, noOfItems = 0, grow = 10;
  private String[] data = new String[maxcap];

  // Default Constructor
  public SortedVector()
  { 
  }

  public void SetGrowBy(int growby)
  {
      grow = growby;
  }


  public int GetCapacity()
  {
    return maxcap;
  }

  public int GetNoOfItems()
  {
    return noOfItems;
  }

  public String GetItemByIndex(int index)
  {

    if (index > noOfItems+1 || index < 0)
    {
      return null;
    }
    else
    {
      String item = data[index];
      return item;
    }
  }



  public int FindItem(String search)
  {
      int low=0;
      int high = noOfItems - 1; 

      return binarySearch(search, low, high);
  }


  public int binarySearch(String search, int low, int high)
  {
      if(low>high)
          return -1;
      int mid = (low + high)/2;
      if (data[mid] == search)
          return mid;
      else 
          if (data[mid].compareToIgnoreCase(search)<0)
              return binarySearch(search, mid+1, high);
          else
              return  binarySearch(search, low, mid-1);
  }



  public void AddItem(String value)
  {
      int thirdCounter = 0;
      int fourthCounter = 0;
      int place3= 0;
      int place4 =0;
      if(maxcap > noOfItems)
      {
          if(noOfItems == 0) 
          {
            data[0] = value;
            noOfItems++;
          }
          else
          {
            int firstCounter = noOfItems;
            for (int i=0; i < firstCounter; i++)
            {
               String[]temp = new String[maxcap]; 

               if(thirdCounter == 0)
               {
                if (data[i].compareToIgnoreCase(value)>0)
                {
                  for (int j=0; j < noOfItems; j++)
                  {
                     temp[j+1] = data[j]; 
                  }
                  data=temp;
                  data[0] = value;
                  noOfItems++;
                  thirdCounter++;
                }  
                else
                {
                    if(data[i].compareToIgnoreCase(value)<0)
                        {
                            for (int j=0; j < noOfItems; j++)
                            {
                               if (data[j].compareToIgnoreCase(value)>0)
                                {
                                    if(fourthCounter ==0)
                                    {
                                        temp[j+1] = data[j];
                                        place3 = j;
                                        fourthCounter++;
                                    }
                                   else
                                    {
                                        temp[j+1] = data[j];                                       
                                    }
                                }
                                else
                                {
                                    temp[j]=data[j];
                                    place4 = j;
                                }
                            }
                           if (place3 == 0)
                            {
                               if(place4 == 0)
                               {
                                data=temp;
                                data[1] = value;
                                noOfItems++;
                                firstCounter++;
                               }
                               else
                               {
                                data=temp;
                                data[place4+1] = value;
                                noOfItems++;
                                thirdCounter++;
                               }
                            }
                            else
                            {
                                data=temp;
                                data[place3] = value;
                                noOfItems++;
                                thirdCounter++;
                            }
                        }
                    }
               }
            }
          }
      }
      else
      {
            int firstCounter = 0;
            maxcap = grow +maxcap;
            String[]temp3 = new String[maxcap]; 
            for (int i=0; i < noOfItems; i++) 
             {
                 if(firstCounter == 0)
                 {
                    if (data[i].compareToIgnoreCase(value)>0) 
                    {
                     for (int j=0; j < noOfItems; j++)
                     {
                         temp3[j+1] = data[j];
                     }
                     data=temp3;
                     data[0] = value;
                     noOfItems++;
                     firstCounter++;
                    }
                    else
                    {  
                        int place1 = 0;
                        int place2 = 0;
                        int secondCounter = 0;
                        if(data[i].compareToIgnoreCase(value)<0)
                        {
                            for (int j=0; j < noOfItems; j++)
                            {
                                if (data[j].compareToIgnoreCase(value)>0)
                                {
                                    if(j/2!=0 && secondCounter ==0)
                                    {
                                        temp3[j+1] = data[j];
                                        place1 = j;
                                        secondCounter++;
                                    }
                                   else
                                    {
                                        temp3[j+1] = data[j];                                        
                                    }
                                }
                                else
                                {
                                    temp3[j]=data[j];
                                    place2 = j;
                                }

                            }
                            if (place1 == 0)
                            {
                               if(place2 == 0)
                               {
                                    data=temp3;
                                    data[1] = value;
                                    noOfItems++;
                                    firstCounter++;
                               }
                               else
                               {
                                    data=temp3;
                                    data[place2+1] = value;
                                    noOfItems++;
                                    firstCounter++;
                               }
                            }
                            else
                            {
                                data=temp3;
                                data[place1] = value;
                                noOfItems++;
                                firstCounter++;
                            }

                        }
                    }
                 }
             }

      }
      System.out.println("adding: "+value);
  }

  public void DeleteItem(int index)
  {
      if (index < noOfItems && index >= 0)
      {
          data[index] = null;
          if (data[index+1] != null)
          {
              int j = index;
              for(int i = (index+1); i<noOfItems; i++)
              {

                  data[j] = data[i];
                  j++;
              }
          }
          noOfItems--;
      }
      System.out.println("deleted: "+index);
  }


  public String toString()
  {
    return super.toString();
  }
}

关于如何做到这一点的任何提示都非常感激。

亲切的问候, 本。

3 个答案:

答案 0 :(得分:1)

实现add,(binaryAdd)几乎与您实现二进制搜索的方式相同。代码将与99%相似。

假设您有以下数据:

+--------------------------+
|10|20|30|40|50|60|70|80|90|
+--------------------------+

您希望在其中添加35并按升序保存数据。

中值为50,并且因为35 <&lt; 50,我们对10到50感兴趣:

+--------------+
|10|20|30|40|50|
+--------------+

中值为30,并且因为35> 30,我们对30到50感兴趣:

+--------+
|30|40|50|
+--------+

中间值为40,并且因为35 <&lt; 40,我们对30到40感兴趣:

+-----+
|30|40|
+-----+

当您离开2个元素时,请选择左侧或右侧进行比较:

if you choose left, 35 > 30, so 35 should be added after 30.
if you choose right, 35 < 40, so 35 should be before after 40.

该过程类似于二分查找,但不是返回目标值的位置,而是返回插入值的位置。

答案 1 :(得分:0)

这是

DEMO https://repl.it/Eqak/0

  public void AddItem(String value)
  {
      // Check if cursor at last
      if(noOfItems + 1 == maxcap){
        // Resize data
        maxcap *= 2;
        String[] newData = new String[maxcap];
        System.arraycopy(data, 0, newData, 0, noOfItems);
        data = newData;
      }

      // find the last element according to value
      int idx = 0;
      for(; idx<noOfItems; idx++){
        if(data[idx].compareToIgnoreCase(value) >= 0) {
          break;
        }
      }

      // move elements if required
      if(idx < noOfItems){
        System.arraycopy(data, idx, data, idx+1, noOfItems-idx);
      }

      // set element on index
      data[idx] = value;

      noOfItems++;

      System.out.println("adding: "+value);
  }

答案 2 :(得分:0)

最简单的方法是,如果找不到元素,则binarySearch返回插入位置。非常像Arrays.binarySearch

  

返回:搜索键的索引(如果它包含在数组中);否则,( - (插入点) - 1)

因此,如果ret=binarySearch的返回为否定,则只需要-ret-1获取插入位置。

你甚至可以看看它的开源the code(即不仅它运行,但你可以用它来学习它)

(不,我不会复制/粘贴那段代码;仅限链接的答案 - 接受或离开它。)