【基本算法-1】二分法排序,查找

二分法排序: 

void Start()
     {
         int[] tempArrayList = new int[11]{3,4,7,3,2,1,11,9,6,1,121};
         HalfSort(tempArrayList);
         for(int i = 0;i < tempArrayList.Length;i ++)
         {
             Debug.Log(tempArrayList[i]);
         }
     }

    public void HalfSort(int[] _array)
    {
        for(int i = 0;i < _array.Length;i++)
        {
            int tempStartIndex = 0;
            //index(i)跟index[0-(i-1)]之前的數組元素比較
            int tempEndIndex = i - 1;
            int tempMiddleIndex;
            int tempCurrentValue = _array[i];
            int tempMiddleValue;
            while(tempStartIndex <= tempEndIndex)
            {
                //index(0-(i-1)) 中間的元素
                tempMiddleIndex = (tempStartIndex + tempEndIndex)/2;
                tempMiddleValue = _array[tempMiddleIndex];
                //index(0-(i-1)) 中間的元素值比index(i)位置的元素值大,說明
                //index(tempMiddleIndex-tempEndIndex)的元素值比 index(i)元素值大
                //結束index直接從tempEndIndex跳到中間元素index(tempMiddleIndex)之前
                //if(tempCurrentValue > tempMiddleValue) 降序
                if(tempMiddleValue > tempCurrentValue)
                {
                    tempEndIndex = tempMiddleIndex-1;
                }else
                {
                    //index(0-(i-1)) 中間的元素值比index(i)位置的元素值小,說明
                    //index(tempStartIndex-tempMiddleIndex)的元素值比 index(i)元素值小
                    //開始index直接從tempStartIndex跳到中間元素index(tempMiddleIndex)之後
                    tempStartIndex = tempMiddleIndex+1;
                }
                //Debug.Log(tempStartIndex + "_" + tempEndIndex);
            }
            //當前要排序index(i) = 7,如果插入位置爲7之前,比如index(4-5)之間(結束while排序時tempEndIndex = 4)
            //將index(5-6)往後移動1個位置,覆蓋當前要排序的index(7),空出index(5),然後index(5) = tempCurrentValue
            for(int j = i-1;j > tempEndIndex;j--)
            {
                _array[j+1] = _array[j];
            }
            //例如:插入位置爲index(4-5)中間,
            //(4+5)/2 = 4 tempCurrentValue > tempMiddleValue; 4 = tempEndIndex = tempMiddleIndex-1;
            //(5+5)/2 = 5 tempCurrentValue < tempMiddleValue; 5 = tempStartIndex = tempMiddleIndex+1;
            // tempStartIndex > tempEndIndex while結束
            _array[tempStartIndex] = tempCurrentValue;
        }
    }

運行結果:

二分法查找:

二分法查找必要條件是數據必須排好序的。

void Start()
     {
        int[] tempArrayList = new int[11]{1,1,2,3,3,4,6,7,9,11,121};
         int tempIndex = SearchSort(tempArrayList,7,0,tempArrayList.Length-1);
         if(tempArrayList[tempIndex] == 7)
         {
            Debug.Log("查找value = 7 index = " + tempIndex + " 查找成功");
         }
     }
  
    public int SearchSort(int[] _array,int _value,int _startIndex,int _endIndex)
    {
        if(_startIndex > _endIndex)
        {
            return - 1;
        }
        int tempMiddleIndex = (_startIndex + _endIndex)/2;
        int tempMiddleValue = _array[tempMiddleIndex];
        //中間位置正好是要查找值,返回index
        if(tempMiddleValue == _value)
        {
            return tempMiddleIndex;
        }
        //tempMiddleValue > _value 說明index(tempMiddleIndex-_endIndex)區間的都大於_value,最大查找index改爲 = tempMiddleIndex - 1)
        if(tempMiddleValue > _value)
        {
            return SearchSort(_array,_value,_startIndex,tempMiddleIndex - 1);
        }else
        {
            return SearchSort(_array,_value,tempMiddleIndex+1,_endIndex);
        }
    }

參考:

https://blog.csdn.net/yysyangyangyangshan/article/details/7399645

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章