public void Add(T data) { RefItem <T> item = new RefItem <T>(data); _innerList.Add(item); _isSorted = false; }
public void Add(T data, out RefItem <T> refItem) { RefItem <T> item = new RefItem <T>(data); refItem = item; if (_isSorted) { int pos = FindProperInsertPos(data); if (pos >= _innerList.Count) { _innerList.Add(item); } else { _innerList.Insert(pos, item); } //SortData(); //int actualPos = BinSearch(item, 0, _innerList.Count - 1); //if (actualPos != pos) //{ //} } else { _innerList.Add(item); } }
public void Delete(RefItem <T> refItem) { //delete specfic node if (_isSorted) { //use binary search to find node //1. find middle point int removeAt = refItem.NodeNumber; for (int i = _innerList.Count - 1; i > removeAt; --i) { _innerList[i].NodeNumber = i - 1; } _innerList.RemoveAt(removeAt); } else { for (int i = _innerList.Count - 1; i >= 0; --i) { if (_innerList[i] == refItem) { _innerList.RemoveAt(i); break; } } } //---------------------------------------------- //delete that item }
int BinSearch(RefItem <T> refItem, int begin, int end) { int pos = begin + ((end - begin) / 2); RefItem <T> sample = _innerList[pos]; if (refItem == sample) { } else { if (sample.Data.CompareTo(refItem.Data) <= 0) { //search down end = end - ((pos - begin) / 2); if (end == begin) { return(-1); } return(BinSearch(refItem, begin, end)); } else { //search up begin = pos + (end - pos) / 2; if (end == begin) { return(-1); } return(BinSearch(refItem, begin, end)); } } return(-1);//not found }
public int Search(RefItem <T> refItem) { if (!_isSorted) { SortData(); } return(BinSearch(refItem, 0, _innerList.Count - 1)); }
public void Add(out RefItem <T> refItem, T data) { RefItem <T> item = new RefItem <T>(data); _innerList.Add(item); _isSorted = false; refItem = item; }
public void Delete(RefItem <T> refItem) { //delete specfic node if (_isSorted) { int pos = BinSearch(refItem, 0, _innerList.Count - 1); if (pos > -1) { _innerList.RemoveAt(pos); } //int actualPos = -1; //for (int i = _innerList.Count - 1; i >= 0; --i) //{ // if (_innerList[i] == refItem) // { // actualPos = i; // break; // } //} //if (pos != actualPos) //{ //} //for (int i = _innerList.Count - 1; i >= 0; --i) //{ // if (_innerList[i] == refItem) // { // _innerList.RemoveAt(i); // break; // } //} } else { for (int i = _innerList.Count - 1; i >= 0; --i) { if (_innerList[i] == refItem) { _innerList.RemoveAt(i); break; } } } //---------------------------------------------- //delete that item }
int BinSearch(RefItem <T> refItem, int begin, int end) { TRY_AGAIN: int pos = begin + ((end - begin) / 2); RefItem <T> sample = _innerList[pos]; if (refItem == sample) { return(pos); } else { if (begin == end) { return(-1);//not found } if (sample.Data.CompareTo(refItem.Data) < 0) { //this is MaxFirst list //data at this pos is lesser than refItem.Data //we need to move to the begin side of the list end = pos - 1; goto TRY_AGAIN; //return BinSearch(refItem, begin, pos - 1); } else { //this is MaxFirst list //data at this pos is greater than refItem.Data //we need to move to the end of this list begin = pos + 1; goto TRY_AGAIN; //return BinSearch(refItem, pos + 1, end); } } }
// static int MaxFirstSort(RefItem <T> t1, RefItem <T> t2) { return(t2.Data.CompareTo(t1.Data)); }