Strategy Design Pattern

In this article, i would like to give example of strategy design pattern implementation. This pattern belongs to  Behavioral Patterns. The idea of this pattern is that the client can change the behavior of algorithm at running time with the one that is still in the same family.

Case:
We want to create performance benchmark of different sorting algorithm. Thus, we must be able to supply the algorithm interchangeably.

using System;
using System.Collections.Generic;

namespace StrategyPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = CreateLongList();

            SortingStrategy sortedStrategy = new SortingStrategy();

            sortedStrategy.SortingAlgorithm = new SelectionSort();
            sortedStrategy.List = new List(list);
            sortedStrategy.Sort();

            sortedStrategy.SortingAlgorithm = new QuickSort();
            sortedStrategy.List = new List(list);
            sortedStrategy.Sort();
        }

        private static List CreateLongList()
        {
            List list = new List();

            Random random = new Random();
            for (int i = 0; i < 50000; i++)
            {
                list.Add(random.Next(1, 50000));
            }

            return list;
        }
    }

    internal interface ISortingAlgorithm
    {
        void Sort(List list);
    }

    internal class SelectionSort : ISortingAlgorithm
    {
        public void Sort(List list)
        {
            int n = list.Count;

            for (int i = 0; i < n - 1; i++)
            {
                int minIdx = i;
                for (int j = i + 1; j < n; j++)
                    if (list[j] < list[minIdx])
                        minIdx = j;

                int temp = list[minIdx];
                list[minIdx] = list[i];
                list[i] = temp;
            }
        }

        public override string ToString()
        {
            return "Selection Sort Algorithm";
        }
    }

    internal class QuickSort : ISortingAlgorithm
    {
        public void Sort(List list)
        {
            Sort(list, 0, list.Count - 1);
        }

        private void Sort(List list, int low, int high)
        {
            if (low < high)
            {
                int pi = Partition(list, low, high);

                Sort(list, low, pi - 1);
                Sort(list, pi + 1, high);
            }
        }

        private int Partition(List list, int low, int high)
        {
            int pivot = list[high];

            int i = (low - 1);
            for (int j = low; j < high; j++)
            {
                if (list[j] <= pivot)
                {
                    i++;

                    int temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }

            int temp1 = list[i + 1];
            list[i + 1] = list[high];
            list[high] = temp1;

            return i + 1;
        }

        public override string ToString()
        {
            return "Quick Sort Algorithm";
        }
    }

    internal class SortingStrategy
    {
        public List List { get; set; }
        public ISortingAlgorithm SortingAlgorithm { get; set; }

        internal void Sort()
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            this.SortingAlgorithm.Sort(List);

            watch.Stop();
            Console.WriteLine(SortingAlgorithm.ToString() + " Running time: " + watch.ElapsedMilliseconds + " Milliseconds");
        }
    }
}

In this code, we provide two implementations of sorting algorithm which are selection sort and quick sort. Theoretically, quick sort is the most efficient algorithm for sorting. We can add more algorithm by implementing ISortingAlgorithm or alternatively we can use abstract class or inheritance. By using Interface or Abstract class, we are allowed to change the algorithm.

The code is also added with stopwatch that is used to measure the running time of each algorithm.

I got this result running in my computer:
Selection Sort Algorithm Running Time: 9746 milliseconds
Quick Sort Algorithm Running Time: 15 milliseconds

Stopwatch

Certainly, the result is in accordance to the theory.

For those interested to the actual code, you can grab from here. Let me know for any feedback regarding the implementation.