Wednesday, January 30, 2013

Tower of Hanoi

(Source: http://en.wikipedia.org/wiki/Tower_of_Hanoi)


The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower,[1] and sometimes pluralised) is a mathematical game orpuzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:
  • Only one disk may be moved at a time.
  • Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
  • No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves

namespace TowerOfHanoi
{
    class Disk: IComparable<Disk>
    {
        public int Size { get; private set; }

        public Disk(int size)
        {
            this.Size = size;
        }

        public int CompareTo(Disk other)
        {
            return this.Size - other.Size;
        }
    }
}


namespace TowerOfHanoi
{
    class Tower
    {
        private Disk[] Peck;
        private int Pointer= -1;
        public int Height { get; private set; }
        public string name { get; private set; }

        public Tower(int Height, string name)
        {
            Peck = new Disk[Height];
            this.Height = Height;
            this.name = name;

        }

        public void Push(Disk d)
        {
            if (!this.isEmpty())
            {
                if (Peck[Pointer].CompareTo(d) > 0)
                {
                    Peck[++Pointer] = d;
                }
                else
                {
                    throw new NotSupportedException("Can't put large disk on small disk");
                }
            }
            else
            {
                Peck[++Pointer] = d;
            }
        }

        public Disk Pop()
        {
            if (!isEmpty())
            {
                return Peck[Pointer--];
            }
            else
            {
                throw new NotSupportedException("Peck is empty");
            }
        }

        public bool isEmpty()
        {
            return Pointer == -1 ? true : false;
        }

        public override string ToString()
        {
            string final = name + "\n";
            for (int i = Pointer; i >= 0; i--)
            {
                string s = "";
                int space = (Peck.Length- Peck[i].Size);

                for (int x = 0; x < space; x++)
                {
                    s += "  ";
                }

                for (int x = 1; x <= Peck[i].Size; x++)
                {
                    s+="____";
                }

                final += s;
                final += "\n";
            }
            return final;
        }
    }
}

namespace TowerOfHanoi
{
    class TowerOfHanoiSolver
    {
        public void Solve(Tower from, Tower to)
        {
            Tower temp = new Tower(from.Height, "TEMP");
            this.Solve(from, to, temp, from.Height);
        }

        private void Solve(Tower from, Tower to, Tower temp, int height)
        {
            if (height == 1)
            {
                to.Push(from.Pop());
            }
            else
            {
                Solve(from, temp, to, height - 1);
                to.Push(from.Pop());
                Solve(temp, to, from, height - 1);
            }
        }
    }
}

namespace TowerOfHanoi
{
    class Program
    {
        static void Main(string[] args)
        {
            Tower from = new Tower(4, "FROM");
            Tower to = new Tower(4 , "TO");


            TowerOfHanoiSolver solver = new TowerOfHanoiSolver();

            from.Push(new Disk(4));
            from.Push(new Disk(3));
            from.Push(new Disk(2));
            from.Push(new Disk(1));

            Console.WriteLine(from.ToString());
            Console.WriteLine("SOLVING..");
            solver.Solve(from, to);
            Console.WriteLine(to.ToString());
            Console.ReadLine();
        }
    }
}

Tuesday, January 29, 2013

Quick Sort


Worst case performanceO(n2)
Best case performanceO(n log n)
Average case performanceO(n log n)

(Source: http://en.wikipedia.org/wiki/Quicksort )

Tip: Randomly shuffle the array before sort to reduce the probability of the Worst case.



class Program
    {
        private void Swap(int[] input, int x, int y)
        {
            var temp = input[x];
            input[x] = input[y];
            input[y] = temp;
        }

        public int[] QuickSort(int[] input)
        {
            this.QuickSort(input, 0, input.Length - 1);
            return input;
        }

        private void QuickSort(int[] input, int low, int high)
        {
            if (low < high)
            {
                int j = Partition(input, low, high);
                QuickSort(input, low, j - 1);
                QuickSort(input, j + 1, high);
            }
        }

        private int Partition(int[] input, int low, int high)
        {
            int i=low+1;
            int j = high;
            // out of the loop if low value is come to the partition point

            while (true)
            {
                while (input[i] < input[low])
                {
                    i++;
                    if (i >= high)
                    {
                        break;
                    }
                }

                while (input[j] > input[low])
                {
                    j--;
                    if (j <= low)
                    {
                        break;
                    }
                }

                if (i >= j)
                {
                    Swap(input, j, low);

                    break;
                }
                else
                {
                    Swap(input, i, j);
                }
            }

            return j;
        }



        static void Main(string[] args)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            int arraySize = 10000000;
            int[] Quick = new int[arraySize];
         

            for (int x = 0; x < Quick.Length; x++)
            {
                Quick[x] = x;
            }

            for (int x = 0; x < Quick.Length; x++)
            {
                int random = r.Next(0, x);
                var temp = Quick[random];
                Quick[random] = Quick[x];
                Quick[x] = temp;
           
            }

            var p = new Program();
            var StopWtch = new System.Diagnostics.Stopwatch();

            StopWtch.Reset();
            StopWtch.Start();
            p.QuickSort(Quick);
            StopWtch.Stop();
            Console.WriteLine("QUICKSORT:   " + StopWtch.Elapsed.TotalSeconds + "sec");

            Console.ReadLine();
        }
    }

Saturday, January 19, 2013

Heap Sort

Heapsort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of the selection sort family. Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(nlog n) runtime. Heapsort is an in-place algorithm, but it is not a stable sort.

(Source: http://en.wikipedia.org/wiki/Heapsort)

    class Program
    {
        private void Swap(int[] input, int x, int y)
        {
            var temp = input[x];
            input[x] = input[y];
            input[y] = temp;
        }

        private int[] HeapSort(int[] input)
        {
            temp = input;

            // make the irregular array into heap format
            for (int i = input.Length / 2; i >= 1; i--)
            {
                sweep(i, input.Length);
            }

            // sort the heap
            for (int N = input.Length; N >= 1; )
            {
                Swap(temp, 0, N - 1);
                N--;
                sweep(1, N);

            }
     
            return input;
        }

        private int[] temp;

        private int getElementOneBasedIndex(int oneBasedIndex)
        {
            return temp[oneBasedIndex - 1];
        }

        private int sweep(int i, int N)
        {
            for ( ; i <= N/2;)
            {
                int leftChild = getElementOneBasedIndex(2 * i);
                int rightChild = int.MinValue;
                if (2 * i + 1 <= N)
                {
                    rightChild = getElementOneBasedIndex(2 * i + 1);
                }
                int maxChild = leftChild > rightChild ? 2 * i : 2 * i + 1;
                if (getElementOneBasedIndex(maxChild) > getElementOneBasedIndex(i))
                {
                    Swap(temp, maxChild-1, i-1);
                }
                i = maxChild;
            }
            return i;
           
        }

       

   
        static void Main(string[] args)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            int arraySize = 10000000;
            int[] Heap = new int[arraySize];
           

            for (int x = 0; x < IteMerge.Length; x++)
            {
                Heap[x] = x;
            }

            for (int x = 0; x < IteMerge.Length; x++)
            {
                int random = r.Next(0, x);
                var temp = Heap[random];
                Heap[random] = IteMerge[x];
                Heap[x] = temp;
            }


            var p = new Program();
            var StopWtch = new System.Diagnostics.Stopwatch();

            StopWtch.Reset();
            StopWtch.Start();
            p.HeapSort(Heap);
            StopWtch.Stop();
            Console.WriteLine("HEAPSORT:    " + StopWtch.Elapsed.TotalSeconds + "sec");

            Console.ReadLine();
        }
    }

Thursday, January 17, 2013

Recursive Merge Sort


class Program
    {
        private void Swap(int[] input, int x, int y)
        {
            var temp = input[x];
            input[x] = input[y];
            input[y] = temp;
        }

        public int[] MergeSort(int[] input)
        {
            int[] temp = new int[input.Length];
            this.RecursiveMergeSort(input, temp, 0, (input.Length - 1) / 2, input.Length - 1);

            return input;
        }

        private void RecursiveMergeSort(int[] input, int[]temp, int low,int mid, int high)
        {
            if (low < high)
            {
                RecursiveMergeSort(input,temp,low, (low + mid) / 2, mid);
                RecursiveMergeSort(input, temp,mid + 1, (mid + 1 + high) / 2, high);
                Merge(input, temp, low, mid, high);

            }
        }

        private void Merge(int[] input, int[] temp, int low, int mid, int high)
        {
            for (int i = low; i <= high; i++)
            {
                temp[i] = input[i];
            }

            int l = low;
            int h = mid + 1 ;
            for (int i = low; i <= high; i++)
            {
                if (h > high)
                {
                    input[i] = temp[l++];
                }
                else if (l > mid)
                {
                    input[i] = temp[h++];
                }
                else
                {
                    if (temp[l] <= temp[h])
                    {
                        input[i] = temp[l++];

                    }
                    else
                    {
                        input[i] = temp[h++];
                    }
                }
            }

        }

       
   
        static void Main(string[] args)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            int arraySize = 10000000;
            int[] Merge= new int[arraySize];
           

            for (int x = 0; x < Quick.Length; x++)
            {
                Merge[x] = x;
            }

            for (int x = 0; x < Quick.Length; x++)
            {
                int random = r.Next(0, x);
                var temp = Merge[random];
                Merge[random] = Merge[x];
                Merge[x] = temp;
            }


            var p = new Program();
            var StopWtch = new System.Diagnostics.Stopwatch();

            StopWtch.Reset();
            StopWtch.Start();
            p.MergeSort(Merge);
            StopWtch.Stop();
            Console.WriteLine("MERGESORT:   " + StopWtch.Elapsed.TotalSeconds + "sec");
            Console.ReadLine();
        }
    }

Thursday, January 3, 2013

Iterative Merge Sort


class Program
    {
        private void Swap(int[] input, int x, int y)
        {
            var temp = input[x];
            input[x] = input[y];
            input[y] = temp;
        }

        private void Merge(int[] input, int[] temp, int low, int mid, int high)
        {
            for (int i = low; i <= high; i++)
            {
                temp[i] = input[i];
            }

            int l = low;
            int h = mid + 1 ;
            for (int i = low; i <= high; i++)
            {
                if (h > high)
                {
                    input[i] = temp[l++];
                }
                else if (l > mid)
                {
                    input[i] = temp[h++];
                }
                else
                {
                    if (temp[l] <= temp[h])
                    {
                        input[i] = temp[l++];

                    }
                    else
                    {
                        input[i] = temp[h++];

                    }
                }
            }
        }

        public int[] InlineMerge(int[] input)
        {
            temp = new int[input.Length];
            // i length of the merging arrays
            for (int i = 1; i < input.Length; i *= 2)
            {
                // j is the starting position of the current merging arrays
                for (int j = 0; j < input.Length; j += 2 * i)
                {
                    if (j + i < input.Length)
                    {
                        int high = j + 2 * i - 1<input.Length?j + 2 * i - 1:input.Length-1;
                        Merge(input, temp, j, j + i - 1,high);
                    }
                }
            }
            return input;
        }

   
        static void Main(string[] args)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            int arraySize = 10000000;
            int[] IteMerge = new int[arraySize];
           
            for (int x = 0; x < IteMerge.Length; x++)
            {
                IteMerge[x] = x;
            }

            for (int x = 0; x < IteMerge.Length; x++)
            {
                int random = r.Next(0, x);
                var temp = IteMerge[random];
                IteMerge[random] = IteMerge[x];
                IteMerge[x] = temp;
            }


            var p = new Program();
            var StopWtch = new System.Diagnostics.Stopwatch();

            StopWtch.Reset();
            StopWtch.Start();
            p.InlineMerge(InlineMerge);
            StopWtch.Stop();
            Console.WriteLine("ITERATIVEMERGE: " + StopWtch.Elapsed.TotalSeconds + "sec");

            Console.ReadLine();
        }
    }