Monday, August 29, 2011

Bubble sort with dry run

Bubble sort dry run. with all steps how bubble sort work. you can count exchange and comparisons also by adding counter in this code. basic and simple code of the bubble sort in c#.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sorting_algo_s
{
    class salgos
    {
        int[] arr = new int[10];
        public void getdata()
        {
            Console.WriteLine("inter value of array");
            //arr = new int[10];
            for (int i = 0; i < 10; i++)
            {
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }
        }
        public void bubblesort()
        {
            for (int i = 0 ; i < 10; i++)
            {
                Console.WriteLine();
                for (int j =0; j < 10; j++)
                {
                    if (arr[i] < arr[j])
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                    Console.Write(arr[j]);
                }
            }
        }

     public void show()
        {
            Console.WriteLine("\nsorted");
            //arr = new int[10];
            for (int i = 0; i < 10; i++)
            {
               // Console.WriteLine(arr[i]);
            }
        }
}


    class Program
    {
        static void Main(string[] args)
        {
            salgos a = new salgos();
            a.getdata();
            a.bubblesort();
            Console.WriteLine("\nhttp://csharpcode13.blogspot.com/");
            a.show();
        }
    }
}







stack code

A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace stack
{
    class stack
    {
        int k;
        int[] stack1;

       public stack(int n)
        {
            k = 0;
            stack1 = new int[n];   
        }
        public void push(int ak)
        {
           
            stack1[k] = ak;
            k++;
           
        }
        public int pop()
        {
            return stack1[k--];
        }
      
        public void show()
        {
           
            for (int i = 0; i < k; i++)
            {
               
                Console.WriteLine("value pushed at stack ={0}", stack1[i]);
             
            }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            stack a = new stack(10);
            a.push(4);
            a.push(3);
            a.push(7);
            a.push(145);
            a.push(105);
            a.push(104);
            a.pop();
            a.show();

        }
    }
}



Saturday, August 27, 2011

Linked list modified code: C#

linked list is a chain of structure or records called nodes. Each node has at least two members, one ofwhich points to the next item or node in the list! These are defined as Single Linked Lists because they only point to the next item, and not the previous. Those that do point to both are called Doubly Linked Lists.


using System;
using System.Collections.Generic;
using System.Text;

namespace linked_list
{
    class node
    {
        public string name;
        public int id;
        public int ennr;
        public node next;

        public node()
        {
            id = 0;
            ennr = 0;
            name = "";
            next = null;
        }
        public node(node n)
        {
            this.id = n.id;
            this.name = n.name;
            this.ennr = n.ennr;
            this.next = null;
        }
        public node(string name1, int id1, int ennr1)
        {
            name = name1;
            id = id1;
            ennr = ennr1;
            next = null;
        }
    }
    class linked_list
    {
        node head, curr, temp;

      public   linked_list()
        {
            head = curr = temp = null;
        }
        public void insertnode(node n)
        {
            if (head == null)
            {
                head = curr = temp = new node(n);
            }
            else
            {
                temp = curr;
                curr = new node(n);
                temp.next = curr;
            }
        }
        public void showdata()
        {
            node a = head;
            while (a != null)
            {
                Console.WriteLine("studnet name:{0}\tid:{1}\t\tEN:{2}", a.name, a.id, a.ennr);
                a = a.next;
            }
        }
      public void deletefirt()
      {
          head = head.next;
      }
        public void deleteall()
        {
            head = null;
        }
        public void insertbefore(int key,node n)
        {
            node run = head;
            node back = head;
            while (run != null)
            {
                if (run.id == key)
                {
                    back.next = new node(n);
                    back.next.next = run;
                }
                back = run;
                run = run.next;
            }
        }
        public void inserthead(node n)
        {
            node a = new node(n);
            a.next = head;
            head = a;

        }
        public void insertlast(node n)
        {
            curr.next = new node(n);
        }
    
        public void deletemid(int key)
        {
            node temp1 = head;
            node temp2 = head;
            node a = head;
            while (a != null)
            {
                if (a.id == key)
                {
                    temp1.next = temp2;
                    // temp2 = a;
                    break;
                }
                temp1 = a;
                a = a.next;
                temp2 = a.next;
                if (head.id == key)
                {
                    head = head.next;
                }
              

                else if (head == null)
                {
                    Console.WriteLine("the list is null");
                }
               // else if(a.){ Console.WriteLine("There is no such value"); }
             
             
            }
        }
        public void search(int id1)
        {
            bool found = false;
            node a = head;
            while (a != null)
            {
                if (a.id == id1)
                {
                    Console.WriteLine("studnet name:{0}\tid:{1}\t\tEN:{2}", a.name, a.id, a.ennr);
                    found = true;
                    break;

                }
                a = a.next;
            }
            if(found == false)
            {
            Console.WriteLine("Id was not found");
            }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            linked_list a = new linked_list();
            string End;
            Console.WriteLine("THE LINKED LIST WORK");
            Console.WriteLine("----------------------------------------");
            string z, h;
            int x, y;

            a.insertnode(new node("human", 120, 020321));
            a.insertnode(new node("person", 43, 4321));
            a.insertnode(new node("test", 83, 66321));
            a.insertnode(new node("click", 1423, 02321));
            a.insertnode(new node("book", 16, 5461));
            a.insertnode(new node("even", 23, 4));
            a.showdata();
           
            do
            {
      
            try
            {
                do
                {
                    Console.WriteLine("DO YOU WANT ADD NODE");
                    Console.WriteLine("----------------------------------------");
                    //Console.WriteLine("Do you want add node press y to add");
                    Console.WriteLine("\nto add at first press 1\nto add at last press 2\nto add at mid press 3");
                    string ss = Console.ReadLine();
                    if (ss == "3")
                    {
                        Console.WriteLine("ADD NODE AT BEFORE");
                        Console.WriteLine("----------------------------------------");
                        Console.WriteLine("Enter the name of student");
                        z = Console.ReadLine();

                        Console.WriteLine("Enter the Enrollment no of student");
                        y = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("int the id of student should put at");
                        x = Convert.ToInt32(Console.ReadLine());
                        int key;
                        Console.WriteLine("node should be placed before");
                        key = Convert.ToInt32(Console.ReadLine());
                        a.insertbefore(key, new node(z, x, y));
                        a.showdata();

                    }
                    else if (ss == "1")
                    {
                        Console.WriteLine("ADD NODE AT HEAD");
                        Console.WriteLine("----------------------------------------");
                        Console.WriteLine("Enter the name of student");
                        z = Console.ReadLine();

                        Console.WriteLine("Enter the Enrollment no of student");
                        y = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("Enter the id of student");
                        x = Convert.ToInt32(Console.ReadLine());
                        a.inserthead(new node(z, x, y));
                        a.showdata();
                    }
                    else if(ss == "2")
                    {
                        Console.WriteLine("ADD NODE AT LAST");
                        Console.WriteLine("----------------------------------------");
                        Console.WriteLine("Enter the name of student");
                        z = Console.ReadLine();

                        Console.WriteLine("Enter the Enrollment no of student");
                        y = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("Enter the id of student");
                        x = Convert.ToInt32(Console.ReadLine());
                        a.insertlast(new node(z, x, y));
                        a.showdata();
                    }

                    Console.WriteLine("----------------------------------------");
                    Console.WriteLine("Enter 0 to end");
                    h = Console.ReadLine();
                    Console.WriteLine("----------------------------------------");
                }
                while (h != "0");

              
                int ff;
                do
                {
                    Console.WriteLine("DO YOU WANT DELTE NODE");
                    Console.WriteLine("----------------------------------------");
                    Console.WriteLine("press 1 delete first node\npress 2 to delete all nodes\npress 3 to delte mid node");
                    ff = Convert.ToInt32(Console.ReadLine());
                    switch (ff)
                    {
                        case 1:
                            Console.WriteLine("DELETE FIRST NODE");
                            Console.WriteLine("----------------------------------------");
                            a.deletefirt();
                            a.showdata();
                            break;
                        case 2:
                            Console.WriteLine("DELETE ALL NODE");
                            Console.WriteLine("----------------------------------------");
                            a.deleteall();
                            a.showdata();
                            break;
                        case 3:
                            Console.WriteLine("DELETE MID NODE");
                            Console.WriteLine("----------------------------------------");
                            Console.WriteLine("Enter the id of student to delete record");
                            int hh = Convert.ToInt32(Console.ReadLine());
                            a.deletemid(hh);
                            a.showdata();
                            break;

                        default: Console.WriteLine("wrong input"); break;
                    }
                   // a.showdata();
                    Console.WriteLine("----------------------------------------");
                    Console.Write("TO END PRESS 0:");
                   
                    ff =Convert.ToInt32( Console.ReadLine());
                    Console.WriteLine("----------------------------------------");

                } while (ff != 0);
            }
            catch (Exception HH)
            {
                Console.WriteLine(HH.Message);
                }
            Console.WriteLine("DO YOU WANT SEARCH STUDENT");
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("Enter the id of student to serch record");
            int id = Convert.ToInt32(Console.ReadLine());
            a.search(id);
                Console.Write("TO END PRESS 0:");
                End = Console.ReadLine(); Console.Clear();
            } while (End != "0");
        }
    }
}




used in operatonal research LP(linear programming) The Simplex Algorithm Simplex method Resolve using the Simple...