[英]Sorted Vector - AddItem
我寫了一個排序的矢量,可以正常工作。 但是我的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();
}
}
我如何做到這一點的任何技巧都值得贊賞。
親切的問候,本。
實現加法(binaryAdd)與實現二進制搜索的方法幾乎相同。 該代碼將有99%相似。
假設您有以下數據:
+--------------------------+
|10|20|30|40|50|60|70|80|90|
+--------------------------+
您要添加35,並將數據按升序排列。
中間值為50,並且由於35 <50,我們對10到50感興趣:
+--------------+
|10|20|30|40|50|
+--------------+
中間值為30,由於35> 30,因此我們對30到50感興趣:
+--------+
|30|40|50|
+--------+
中間值為40,並且由於35 <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.
該過程類似於二進制搜索,但是您無需返回目標值的位置,而是返回插入值的位置。
這里是
演示 : 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);
}
最簡單的方法是,如果找不到該元素,則使binarySearch返回插入位置。 就像Arrays.binarySearch一樣:
返回:搜索鍵的索引,如果它包含在數組中; 否則,(-(插入點)-1)
因此,如果ret=binarySearch
的返回值為負,則只需采用-ret-1
即可獲得插入位置。
你甚至可以去看一下代碼 ,其開源反正(即它不僅運行,但你可以用它從中學習)
(不,我不會復制/粘貼這段代碼;僅鏈接的答案-接受或保留它)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.