Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace insertion_sort
{
    class Program
    {
        static int[] A;
        //int mid = 150;
        //static int[] R;
       // static int[] numbers;
        static Thread t1;
        static Thread t2;
        static Thread t3;
        static ThreadStart ts1;
        static ThreadStart ts2;
        static ThreadStart ts3;

        public static void insertion_sort1()
        {
            A = new int[100];

            Random r = new Random();
            for (int i = 0; i < 49; i++)
            {
                A[i] = r.Next(1, 100);
            }


            for (int i = 0; i <= 49; i++)
            {
                int j = i;

                while (j != 0 && A[j - 1] > A[j])
                {
                    int temp = A[j - 1];
                    A[j - 1] = A[j];
                    A[j] = temp;

                    j--;
                }
            }
        }

        public static void insertion_sort2()
        {
            Random r = new Random();
            for (int i = 50; i < 99; i++)
            {
                A[i] = r.Next(1, 100);
            }

            for (int i = 50; i <= 99; i++)
            {
                int j = i;

                while (j != 0 && A[j - 1] > A[j])
                {
                    int temp = A[j - 1];
                    A[j - 1] = A[j];
                    A[j] = temp;

                    j--;
                }
            }
        }

        static public void DoMerge()
        {
            int left = 0;
            int mid =50;
            int right = 99;
            A = new int[100];

            int[] temp = new int[150];
            int i, left_end, num_elements, tmp_pos;

            left_end = (mid - 1);
            tmp_pos = left;
            num_elements = (right - left + 1);

            while ((left <= left_end) && (mid <= right))
            {
                if (A[left] <= A[mid])
                    temp[tmp_pos++] = A[left++];
                else
                    temp[tmp_pos++] = A[mid++];
            }

            while (left <= left_end)
                temp[tmp_pos++] = A[left++];

            while (mid <= right)
                temp[tmp_pos++] = A[mid++];

            for (i = 0; i < num_elements; i++)
            {
                A[right] = temp[right];
                right--;
            }
        }

        


   
        static void Main(string[] args)
        {
          
            ts1 = new ThreadStart(insertion_sort1);
           
            ts2 = new ThreadStart(insertion_sort2);
       
            ts3 = new ThreadStart(DoMerge);


            t1 = new Thread(ts1);
            t2 = new Thread(ts2);
            t3 = new Thread(ts3);

            t1.Start();
            t2.Start();
            t3.Start();
            t3.Join();  

        }
    }
}
Posted
Updated 26-May-15 7:13am
v2
Comments
PIEBALDconsult 26-May-15 13:12pm    
Start by compiling it.
ZurdoDev 26-May-15 13:38pm    
Create a new console app in Visual Studio and put the code in it.
Sascha Lefèvre 26-May-15 13:49pm    
If you're a beginner you should postpone the topic of threads. There's a whole lot of stuff that's much more important for a beginner.
Sergey Alexandrovich Kryukov 26-May-15 13:54pm    
No, you don't behave like a beginner. Beginners usually ask how to write code in some cases, often very simple ones. And you are only asking about how to run some code. Did you write it? If not, you are yet to become a beginner. You are not one of them yet. Please come back when you feel you are.
—SA

1 solution

Start by going back to where you copied the code from and looking there: just grabbing code from the internet without knowing what it does is a very dangerous way to proceed.
Bear in mind that unless you know what a chunk of code does, running it puts your whole life at risk: it could erase all the files on your hard drive, send your credit card details to the author, email spam to everyone you know, or just order a pizza and have it delivered to you in thirty minutes.

As a beginner who doesn't even know how to compile and run code you shouldn't really do this, any more than you would leave your car keys in the ignition and the drivers door open overnight.

And to be honest, submitting that as your homework is not going to get you a good grade: it might get you kicked off the course for plagiarism however.

Try and write the code yourself - you will learn a lot more!
 
Share this answer
 
Comments
Member 11488595 26-May-15 15:51pm    
i think i asked my question wrong!! i mean i can compiled this code but before debugging the black window was hidden without any error .
OriginalGriff 26-May-15 16:34pm    
:laugh:
Well, that's probably because you don't do any output, or input... So your app runs, does it's bit, and exits. Then there is nothing to look at, so the console window closes.

Either put a line at the end of Main:

string s = Console.ReadLine();

Or put a breakpoint on the first line in the method and then step through your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900