Part 2- -[implementation] - C# code of basic algorithms

 c# code of bubble sort.
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = { 3, 5, 1, 8, 4, 9, 7 };
            bubblesoft(x, 7);

            foreach (int a in x)
                Console.WriteLine(a);

            Console.ReadKey();

        }
        private static void bubblesoft(int[] a, int length)
        {
            bool notsorted = true;

            while(notsorted)
            {
                notsorted = false;
                for(int i=1; i<length; i++)
                {
                    //swap two element
                    if (a[i]<a[i-1])
                    {
                        int temp = 0;
                        temp = a[i];
                        a[i] = a[i - 1];
                        a[i - 1] = temp;
                        notsorted = true;

                    }
                }
            }

        }
    }
 c# code of binary binary.

1. Building binary tree
  class node
    {
       public string name;
       public node leftnode;
       public node rightnode;

        public node(string inname)
        {
            name = inname;
        }

    }

2. Traverseling through nodes
      private static void traverselpreorder(node innode)
        {

            if (innode.leftnode != null)
            {
                Console.WriteLine("Left node: "+ innode.leftnode.name);
                traverselpreorder(innode.leftnode);
            }
            if(innode.rightnode!=null)
            {
                Console.WriteLine("Right node:" + innode.rightnode.name);
                traverselpreorder(innode.rightnode);

            }
        }

3. Implementing

            node root = new node("3");
            node node1 = new node("5");
            node node2 = new node("6");
            node node3 = new node("2");
            node node4 = new node("7");
            node node5 = new node("1");
            node node6 = new node("0");

            root.leftnode = node1;
            root.rightnode = node2;

            node1.leftnode = node3;
            node1.rightnode = node4;

            node3.leftnode = node5;
            node3.rightnode = node6;

            traverselpreorder(root);


Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment