Example #1
0
    //t data的的下标,将data[t]的向下调整
    private void ShiftDown(int t)
    {
        while (2 * t <= count)
        {
            int j = 2 * t;            //左孩子下标
            //存在右孩子且右孩子大于左孩子,则交换的下标j改为右孩子
            if (j + 1 <= count && data[indexes[j]].CompareTo(data[indexes[j + 1]]) < 0)
            {
                j += 1;
            }

            if (data [indexes[t]].CompareTo(data [indexes[j]]) > 0)
            {
                break;
            }
            else
            {
                AlgorithmsHelp.Swap(ref indexes[t], ref indexes[j]);
                //不对的
//				AlgorithmsHelp.Swap (ref reserves[t],ref reserves[j]);
                //对的
                reserves[indexes[t]]   = t;
                reserves [indexes [j]] = j;


                t = j;
            }
        }
    }
Example #2
0
    //对数组arr[left...right]进行划分,双路划分
    private int _Partition2 <T>(T[] arr, int left, int right) where T : System.IComparable <T>
    {
        int randomIndex = Random.Range(left, right + 1);

        AlgorithmsHelp.Swap(ref arr [left], ref arr [randomIndex]);

        //arr[left + 1...i)小于,等于v,arr(j,right]大于等于v
        T   v = arr[left];
        int i = left + 1;
        int j = right;

        while (true)
        {
            while (i <= right && arr [i].CompareTo(v) < 0)
            {
                i++;
            }
            while (j >= left + 1 && arr [j].CompareTo(v) > 0)
            {
                j--;
            }
            if (i > j)            //这里需要记得
            {
                break;
            }

            AlgorithmsHelp.Swap(ref arr[i], ref arr[j]);
            i++;            //这里犯过错误,记得
            j--;            //这里犯过错误,记得
        }
        AlgorithmsHelp.Swap(ref arr[left], ref arr[j]);
        return(j);
    }
Example #3
0
    public static void SelectionSort <T>(T[] arr, int n) where T : IComparable
    {
//		for (int i = 0; i != n; i++) {
//			int min = -300;
//			int index = i;
//			for (int j = i+1; j != n; j++) {
//				if (min > arr [j]) {
//					index = j;
//					min = arr [j];
//				}
//			}
//			int temp = arr [i];
//			arr [i] = arr [index];
//			arr [index] = temp;
////			AlgorithmsHelp.Swap (i,index,arr);
//
//		}//错了。写在这里,让我记一辈子吧。

        for (int i = 0; i != n; i++)
        {
            int minIndex = i;
            for (int j = minIndex + 1; j != n; j++)
            {
                if (arr [j].CompareTo(arr[minIndex]) < 0)
                {
                    minIndex = j;
                }
            }
            AlgorithmsHelp.Swap(ref arr[i], ref arr[minIndex]);
        }
    }
Example #4
0
    private void ShiftUp(int count)
    {
        int t = count;

        while (t > 1 && data [t].CompareTo(data [t / 2]) > 0)
        {
            AlgorithmsHelp.Swap(ref data[t], ref data[t / 2]);
            t = t / 2;
        }
    }
Example #5
0
    // 向上调整位置,直到父母节点小于原来的data[index]
    private void _ShiftUp(int index)
    {
        int t = index;

        while (t / 2 >= 1 && data[t / 2].CompareTo(data[t]) > 0)
        {
            AlgorithmsHelp.Swap(ref data[t / 2], ref data[t]);

            t /= 2;
        }
    }
Example #6
0
    // 向上调整位置,直到父母节点小于原来的data[index]
    private void _ShiftUp(int index)
    {
        int t = index;

        while (t / 2 >= 1 && data[indexes[t / 2]].CompareTo(data[indexes[t]]) > 0)
        {
            AlgorithmsHelp.Swap(ref indexes[t / 2], ref indexes[t]);
            reserves [indexes [t / 2]] = t / 2;
            reserves [indexes [t]]     = t;
            t /= 2;
        }
    }
Example #7
0
    public Item ExtraMinItem()
    {
        Debug.Assert(count - 1 >= 0);
        Item res = data [indexes[1]];

        AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]);
        reserves [indexes [1]]   = 1;
        reserves[indexes[count]] = 0;
        count--;
        _ShiftDown(1);
        return(res);
    }
Example #8
0
    private void ShiftUp(int count)
    {
        int t = count;

        while (t > 1 && data [indexes[t]].CompareTo(data [indexes[t / 2]]) > 0)
        {
            AlgorithmsHelp.Swap(ref indexes[t], ref indexes[t / 2]);
//			AlgorithmsHelp.Swap (ref reserves[t],ref reserves[t/2]);
            reserves[indexes[t / 2]] = t / 2;
            reserves [indexes [t]]   = t;
            t = t / 2;
        }
    }
Example #9
0
    public int ExtraMinItemIndex()
    {
        Debug.Assert(count - 1 >= 0);
        int res = indexes[1] - 1;

        AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]);

        reserves[indexes[1]]      = 1;
        reserves [indexes[count]] = 0;       //0
        count--;
        _ShiftDown(1);
        return(res);
    }
Example #10
0
 public void BubblableSort <T>(T[] arr, int n) where T : IComparable <T>
 {
     while (n != 0)
     {
         for (int i = 0; i != n - 1; i++)
         {
             if (arr [i].CompareTo(arr [i + 1]) > 0)
             {
                 AlgorithmsHelp.Swap(ref arr[i], ref arr[i + 1]);
             }
         }
         n--;
     }
 }
Example #11
0
    //取出最大的元素的索引
    public int ExtractBigItemIndex()
    {
        Debug.Assert(count > 0);

        int ret = indexes[1] - 1;

        AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]);
        reserves [indexes [count]] = 0;
        reserves [indexes [1]]     = 1;
        count--;
        ShiftDown(1);

        return(ret);
    }
Example #12
0
    //对数组arr进行划分,a[left+1...j-1],a[j],a[j+1...right];
    // 1.a[left+1...j] < v, a[j+1...right]>v
    private int Partition(Edge <float>[] arr, int left, int right)
    {
        Edge <float> v = arr [left];
        int          j = left;

        //1.a[left+1...j] < v ,a[j+1...right]>v
        for (int i = left + 1; i <= right; i++)
        {
            if (arr [i].CompareTo(v) < 0)
            {
                AlgorithmsHelp.Swap(ref arr[j + 1], ref arr[i]);
                j++;
            }
        }
        AlgorithmsHelp.Swap(ref arr[j], ref arr[left]);
        return(j);
    }
Example #13
0
    //不需要开辟新的空间,进行的原地堆排序
    public void HeapSort3 <T>(T[] arr, int n) where T : System.IComparable <T>
    {
        int count = n;
        int t     = (count - 1) / 2;

        while (0 <= t)
        {
            _ShiftDown(arr, n, t);
            t--;
        }

        for (int i = n - 1; i >= 1; i--)
        {
            AlgorithmsHelp.Swap(ref arr[i], ref arr[0]);
            _ShiftDown(arr, i, 0);           //这里又犯错了。。。
        }
    }
Example #14
0
 public static void InsertionSort <T>(T[] arr, int n) where T : IComparable
 {
     for (int i = 1; i != n; i++)
     {
         for (int j = i; j > 0; j--)
         {
             if (arr [j].CompareTo(arr [j - 1]) < 0)
             {
                 AlgorithmsHelp.Swap(ref arr [j], ref arr [j - 1]);
             }
             else
             {
                 break;
             }
         }
     }
 }
Example #15
0
    //优化过的bubble
    public void BubblableSort1 <T>(T[] arr, int n) where T : IComparable <T>
    {
        bool swapped = false;

        do
        {
            swapped = false;
            for (int i = 1; i != n; i++)
            {
                if (arr [i].CompareTo(arr [i - 1]) < 0)
                {
                    AlgorithmsHelp.Swap(ref arr [i], ref arr [i - 1]);
                    swapped = true;
                }
            }
            n--;
        } while(swapped);
    }
Example #16
0
    //对arr[left...right]进行三路快速排序
    public static void _QuickSort3 <T> (T[] arr, int left, int right) where T : System.IComparable <T>
    {
        if (right - left <= 15)
        {
            _InsertSort(arr, left, right);
            return;
        }

        //Partition
        //arr[left+1...lj] < v , arr[lj+1...i-1]=v , arr[rj...right] < v
        int randomIndex = Random.Range(left, right + 1);

        AlgorithmsHelp.Swap(ref arr[randomIndex], ref arr[left]);
        T   v  = arr[left];
        int lj = left;
        int rj = right + 1;
        int i  = left + 1;

        while (i < rj)
        {
            if (arr [i].CompareTo(v) < 0)
            {
                AlgorithmsHelp.Swap(ref arr[lj + 1], ref arr[i]);
                lj++;
                i++;
            }
            else if (arr [i].CompareTo(v) > 0)
            {
                AlgorithmsHelp.Swap(ref arr[i], ref arr[rj - 1]);
                rj--;
            }
            else if (arr [i].CompareTo(v) == 0)
            {
                i++;
            }
        }
        //
        AlgorithmsHelp.Swap(ref arr[left], ref arr[lj]);

        _QuickSort3(arr, left, lj - 1);
        _QuickSort3(arr, rj, right);
    }
Example #17
0
 private void _ShiftDown <T>(T[] arr, int n, int t) where T : System.IComparable <T>
 {
     while (2 * t + 1 <= n - 1)     //这里犯错误了
     {
         int j = 2 * t + 1;
         if (j + 1 <= n - 1 && arr [j].CompareTo(arr [j + 1]) < 0)
         {
             j += 1;
         }
         if (arr [t].CompareTo(arr [j]) > 0)
         {
             break;
         }
         else
         {
             AlgorithmsHelp.Swap(ref arr[t], ref arr[j]);
             t = j;
         }
     }
 }
Example #18
0
    // 对a[left...right]进行划分
    private int _Partition <T>(T[] arr, int left, int right) where T : System.IComparable <T>
    {
        int randomIndex = Random.Range(left, right + 1);      //n很大,而且基本为有序时会出问题

        AlgorithmsHelp.Swap(ref arr[left], ref arr[randomIndex]);

        T   v = arr [left];
        int j = left;

        //a[left+1...j]均小于v,而a[j+1...right]均大于v
        for (int i = left + 1; i <= right; i++)
        {
            if (arr [i].CompareTo(v) < 0)
            {
                AlgorithmsHelp.Swap(ref arr [j + 1], ref arr [i]);
                j++;
            }
        }
        AlgorithmsHelp.Swap(ref arr[left], ref arr[j]);
        return(j);
    }
Example #19
0
    //t data的的下标,将data[t]的向下调整
    private void ShiftDown(int t)
    {
        while (2 * t <= count)
        {
            int j = 2 * t;            //左孩子下标
            //存在右孩子且右孩子大于左孩子,则交换的下标j改为右孩子
            if (j + 1 <= count && data[j].CompareTo(data[j + 1]) < 0)
            {
                j += 1;
            }

            if (data [t].CompareTo(data [j]) > 0)
            {
                break;
            }
            else
            {
                AlgorithmsHelp.Swap(ref data[t], ref data[j]);
                t = j;
            }
        }
    }
Example #20
0
    //向下调整位置,直到该节点处于合适的位置
    private void _ShiftDown(int index)
    {
        int t = index;

        while (t * 2 <= count)
        {
            int j = t * 2;
            if (j + 1 <= count && data [j + 1].CompareTo(data [j]) < 0)
            {
                j = j + 1;
            }
            if (data [t].CompareTo(data [j]) < 0)
            {
                break;
            }
            else
            {
                AlgorithmsHelp.Swap(ref data [t], ref data [j]);
                t = j;
            }
        }
    }
Example #21
0
    //向下调整位置,直到该节点处于合适的位置
    private void _ShiftDown(int index)
    {
        int t = index;

        while (t * 2 <= count)
        {
            int j = t * 2;
            if (j + 1 <= count && data [indexes[j + 1]].CompareTo(data [indexes[j]]) < 0)
            {
                j = j + 1;
            }
            if (data [indexes[t]].CompareTo(data [indexes[j]]) < 0)
            {
                break;
            }
            else
            {
                AlgorithmsHelp.Swap(ref indexes[t], ref indexes[j]);
                reserves [indexes [t / 2]] = t / 2;
                reserves [indexes [t]]     = t;
                t = j;
            }
        }
    }