コード例 #1
0
ファイル: Solution.cs プロジェクト: traiv/Courses
        // Implements concurrent version of MergeSort.
        public override void sortCon(int[] d)
        {
            // Todo 1: Instantiate an object of mergeSort.
            SequentialMergeSort mergeSort = new SequentialMergeSort(d);

            mergeSort.printContent("\n Before the concurrent merge sort ");

            // Todo 2: Divide the main array into two pieces: left and right. Where is the middle?
            int midPos = d.Length / 2;

            // Todo 3: Give the tasks. Each thread sorts one piece independent from the other.
            Thread leftSort  = new Thread(() => mergeSort.sortSeq(0, midPos));
            Thread rightSort = new Thread(() => mergeSort.sortSeq(midPos + 1, d.Length - 1));

            // Todo 4: Start the threads.
            leftSort.Start();
            rightSort.Start();

            // Todo 5: Join to the working threads.
            leftSort.Join();
            rightSort.Join();

            // Todo 6: Merge the results to create the complete sorted array. Then print the content
            mergeSort.merge(0, midPos, d.Length - 1);
            mergeSort.printContent("\n After the concurrent merge sort ");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: joeeri/Concurrency
        static void Main(string[] args)
        {
            int[] arr = { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16, 3, 100, 83, 24, 18, 33, 44, 76 };


            SequentialMergeSort mergeSort = new SequentialMergeSort(arr);

            mergeSort.printContent();
            mergeSort.sortSeq(0, arr.Length - 1);
            mergeSort.printContent();

            // uncomment this only if the solution is available
            //Console.WriteLine("\n Now concurrent sort will be running ...");
            //SolutionConcurrentMergeSort concMergeSort = new SolutionConcurrentMergeSort();
            //concMergeSort.sortCon(arr);
        }