コード例 #1
0
ファイル: Program.cs プロジェクト: domgud/Lab1_MergeSort
        // Function  for 3-way merge sort process
        public static void mergeSort3Way(MyFileArray gArray)
        {
            // if array of size is zero returns null
            if (gArray == null)
            {
                return;
            }

            // creating duplicate of given array
            // copying elements of given array into
            // duplicate array
            MyFileArray fArray = new MyFileArray(@"mydataarraycopy.dat", gArray);

            // sort function
            mergeSort3WayRec(fArray, 0, gArray.Length, gArray);
            // copy back elements of duplicate array
            // to given array
            for (int i = 0; i < fArray.Length; i++)
            {
                gArray.Replace(i, fArray[i]);
            }
            fArray.fs.Close();
            File.Delete(@"mydataaarraycopy.dat");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: domgud/Lab1_MergeSort
        /* Merge the sorted ranges [low, mid1), [mid1,
         * mid2) and [mid2, high) mid1 is first midpoint
         * index in overall range to merge mid2 is second
         * midpoint index in overall range to merge*/
        public static void merge(MyFileArray gArray, int low,
                                 int mid1, int mid2, int high,
                                 MyFileArray destArray)
        {
            int i = low, j = mid1, k = mid2, l = low;

            // choose smaller of the smallest in the three ranges
            while ((i < mid1) && (j < mid2) && (k < high))
            {
                if (gArray[i].CompareTo(gArray[j]) < 0)
                {
                    if (gArray[i].CompareTo(gArray[k]) < 0)
                    {
                        destArray.Replace(l++, gArray[i++]);
                    }

                    else
                    {
                        destArray.Replace(l++, gArray[k++]);
                    }
                }
                else
                {
                    if (gArray[j].CompareTo(gArray[k]) < 0)
                    {
                        destArray.Replace(l++, gArray[j++]);
                    }
                    else
                    {
                        destArray.Replace(l++, gArray[k++]);
                    }
                }
            }

            // case where first and second ranges have
            // remaining values
            while ((i < mid1) && (j < mid2))
            {
                if (gArray[i].CompareTo(gArray[j]) < 0)
                {
                    destArray.Replace(l++, gArray[i++]);
                }
                else
                {
                    destArray.Replace(l++, gArray[j++]);
                }
            }

            // case where second and third ranges have
            // remaining values
            while ((j < mid2) && (k < high))
            {
                if (gArray[j].CompareTo(gArray[k]) < 0)
                {
                    destArray.Replace(l++, gArray[j++]);
                }

                else
                {
                    destArray.Replace(l++, gArray[k++]);
                }
            }

            // case where first and third ranges have
            // remaining values
            while ((i < mid1) && (k < high))
            {
                if (gArray[i].CompareTo(gArray[k]) < 0)
                {
                    destArray.Replace(l++, gArray[i++]);
                }
                else
                {
                    destArray.Replace(l++, gArray[k++]);
                }
            }

            // copy remaining values from the first range
            while (i < mid1)
            {
                destArray.Replace(l++, gArray[i++]);
            }

            // copy remaining values from the second range
            while (j < mid2)
            {
                destArray.Replace(l++, gArray[j++]);
            }

            // copy remaining values from the third range
            while (k < high)
            {
                destArray.Replace(l++, gArray[k++]);
            }
        }