Visar inlägg med etikett recursion. Visa alla inlägg
Visar inlägg med etikett recursion. Visa alla inlägg

måndag 23 juni 2014

Merge sort in C#

Merge sort - overview

Merge sort is a comparison based divide-and-conquer sorting algoritm. It was invented by John von Neumann in 1945. It is also a stable algorithm, which means that the order of equal elements in the input array will be kept so that it is the same after sorting.

The algorithm is based on the fact that two ordered arrays can be merged pretty quickly and easy. One just loops through the two arrays in parallel och copies the smallest value of the two arrays into a third array. The algorithm then uses recursive divide-and-conquer methodology to get the sorting done.

Description of the merge sort algorithm

The algorithm works like this:
  1. The unsorted input array is recursively divided into n subarrays containing one element each. A list with only 1 element is considered to be sorted.
  2. The subarrays are merged to produce new sorted subarrays until there is only one array left.

Computational complexity

In each recursion level merging is performed. Since merging is a linear operation, at each level we are doing O(n) work.

In each recursive step we are dividing the size of the input in half. Therefore there will be O(log n) recursive calls.

The number of recursive calls multiplied with the work at each level thus gives us the total complexity of O(n log n).

Example of an implementation of merge sort in C#


    private void SortArrayWithMergeSort()
    {
        int[] array = { 7, 5, 17, 3, 1 };

        int low = 0;
        int hi = array.Length - 1;

        MergeSort(array, low, hi);

        // PrintResult(array);
    }

    public void MergeSort(int[] array, int low, int hi)
    {
        if (hi - low <= 0)
        {
            return;
        }

        int mid = (low + hi) / 2;
        MergeSort(array, low, mid);
        MergeSort(array, mid + 1, hi);
        Merge(array, low, mid + 1, hi);
    }

    private void Merge(int[] array, int low, int mid, int hi)
    {
        int[] temp = new int[array.Length];
        int tempCount = low;
        int i = low;
        int j = mid;

        while (i < mid && j <= hi)
        {
            if (array[i] <= array[j])
            {
                temp[tempCount++] = array[i++];
            }
            else
            {
                temp[tempCount++] = array[j++];
            }
        }

        while (i < mid)
        {
            temp[tempCount++] = array[i++];
        }

        while (j <= hi)
        {
            temp[tempCount++] = array[j++];
        }

        for (int k = low; k <= hi; k++)
        {
            array[k] = temp[k];
        }   
    }




fredag 20 juni 2014

Quicksort in C#

Quicksort - overview

The quicksort algorithm is a very elegant and fast sorting algorithm. It was named "quicksort" by its originator Tony Hoare, who developed it in 1960.

Quicksort is a good example of the general problem solving heuristic divide and conquer. The basic idea of such an algorithm is to split data into two or more sub-problems and then solving the parts individually and in the end combine the part solutions to get the full solution.

The algorithm can be used to sort an array of for example integers or strings.

Description of the quicksort algorithm

The first step in the algorithm is to choose a pivot element. A simple choice could be to pick the leftmost element of the partition, or as in the C# example below to use the partitions middle index.

In the next step it places elements that are larger than the pivot on the right side of the pivot. In the same manner it then places elements that are less than or equal to the pivot on the left side.

At this stage the array is split up into two partitions and the array is sorted to some extent. All values in the right partition are larger than the all values in the left partition.

Now the algorithm can continue to apply quicksort recursively on each of the two partitions. For each partition the algorithm will pick a new pivot and then separate the values in two new smaller partitions. 

The recursion will go on until the base case is reached. The base case occurs when the arrays have been reduced to a size of 1 or zero, since that small arrays never need to be sorted.

Example of an implementation of quicksort in C#


    private void SortArrayWithQuickSort()
    {
        IComparable[] arrayToSort = new String[] { "d", "c", "r", "q", "b" };
        int minIndex = 0;
        int maxIndex = arrayToSort.Length - 1;
        Quicksort(arrayToSort, minIndex, maxIndex);

        // PrintResult(arrayToSort);
    }

    public void Quicksort(IComparable[] elements, int left, int right)
    {
        int i = left;
        int j = right;
        IComparable pivotValue = GetMiddleValue(elements, left, right);

        while (i <= j)
        {
            while (ArrayValueIsLessThanPivot(elements, i, pivotValue))
            {
                i++;
            }

            while (ArrayValueIsGreaterThanPivot(elements, j, pivotValue))
            {
                j--;
            }

            if (i <= j)
            {
                SwapValues(elements, i, j);
                i++;
                j--;
            }
        }

        if (left < j)
        {
            Quicksort(elements, left, j);
        }

        if (i < right)
        {
            Quicksort(elements, i, right);
        }
    }

    private static void SwapValues(IComparable[] elements, int i, int j)
    {
        IComparable tmp = elements[i];
        elements[i] = elements[j];
        elements[j] = tmp;
    }

    private static bool ArrayValueIsGreaterThanPivot(IComparable[] elements, 
        int j, 
        IComparable pivotValue)
    {
        return elements[j].CompareTo(pivotValue) > 0;
    }

    private static bool ArrayValueIsLessThanPivot(IComparable[] elements, 
        int i, 
        IComparable pivotValue)
    {
        return elements[i].CompareTo(pivotValue) < 0;
    }

    private static IComparable GetMiddleValue(IComparable[] elements, 
        int left, 
        int right)
    {
        int middleIndex = left + (right - left) / 2;
        return elements[middleIndex];
    }


Computational complexity

The asymptotic execution time for the quicksort algorithm is for average and best case O(n log n).
For worst case it is O(n2).