Ejemplo n.º 1
0
        private int Select(int[] arr, int k, TabControl tabControl)
        {
            if (arr.Length <= 10)
            {
                return(Find(CopyArray(arr), k));
            }

            int        pivotNumber = ChooseWisePivot(CopyArray(arr), tabControl);
            List <int> copy        = arr.ToList();
            int        pivotIndex  = Partition(ref arr, pivotNumber);

            TabControl t    = null;
            string     page = "Partition";

            GUI_Manager.AddPage(tabControl, page);
            t = GUI_Manager.AddPartition(tabControl, page, copy, arr.ToList(), pivotIndex, pivotNumber, k);

            if (k <= pivotIndex)
            {
                int[] a1 = CreateArraySmaller_BiggerThanPivot(CopyArray(arr), pivotNumber, true);

                if (a1.Length <= 10)
                {
                    GUI_Manager.AddFinalResult(t ?? tabControl, a1.ToList(), k);
                }

                int right = Select(CopyArray(a1), k, t ?? tabControl);
                return(right);
            }
            else
            {
                int[] a2 = CreateArraySmaller_BiggerThanPivot(CopyArray(arr), pivotNumber, false);

                if (a2.Length <= 10)
                {
                    GUI_Manager.AddFinalResult(t ?? tabControl, a2.ToList(), k - pivotIndex);
                }

                int left = Select(CopyArray(a2), k - pivotIndex, t ?? tabControl);
                return(left);
            }
        }
Ejemplo n.º 2
0
        private int ChooseWisePivot(int[] arr, TabControl tabControl)
        {
            GUI_Manager.AddArrayPage(tabControl, arr.ToList());

            string   page = "Split Array";
            TabPage  p    = GUI_Manager.AddPage(tabControl, page);
            TreeView tv   = GUI_Manager.CreateTreeView(p);

            List <int> b = new List <int>();

            int iMax = (int)Math.Ceiling(arr.Length / 5.0);

            int[] m = new int[iMax];
            int   index = 0, i = 0;

            while (i != arr.Length)
            {
                b.Add(arr[i++]);

                if (b.Count == 5)
                {
                    page = index.ToString();

                    GUI_Manager.AddList(tv, b, page);

                    b.Sort();
                    m[index++] = b[2];
                    b.Clear();
                }
            }

            if (iMax != index)
            {
                page = index.ToString();

                GUI_Manager.AddList(tv, b, page);

                b.Sort();
                m[index] = b[(int)Math.Floor(b.Count / 2.0)];
            }

            tv.ExpandAll();

            TabControl tControl = null;
            int        half     = (int)Math.Ceiling(m.Length / 2.0) - 1;
            int        meridian = -1;

            if (iMax > 10)
            {
                page = "Select Split Array";
                GUI_Manager.AddPage(tabControl, page);
                tControl = GUI_Manager.AddTabControl(tabControl, page);
                meridian = Select(CopyArray(m), half, tControl);
            }
            else
            {
                meridian = Select(CopyArray(m), half, tControl);
                GUI_Manager.AddSummarySplit(tabControl, m.ToList(), meridian);
            }

            return(meridian);
        }