Sunday, July 29, 2018

Sorting Algorithm in C#


In this section, we’re going to take a look at a number of well-known sorting algorithms with the hope of sensitizing you to the notion of performance–a topic that is covered in greater detail in courses such as algorithms and data structures.

Bubble Sort :-  The bubble is the very common technique for beginners. It is easy to implement and understand. In bubble sort, each element of the unsorted list is compared to the next element and if the value of first element is greater than the value of the second element, then they are swapped.

Example :- 

Public static void BubbleShort()
        {
            var intArray = new int[5] {3, 21, 4, 8, 6};
            int temp;

            for (int i = 1; i <= intArray.Length; i++)
            {
                for (int j = 0; j < intArray.Length - i; j++)
                {
                    if (intArray[j] > intArray[j + 1])
                    {
                        temp = intArray[j];
                        intArray[j] = intArray[j + 1];
                        intArray[j + 1] = temp;
                    }
                }
            }
            foreach (var i in intArray)
            {
                Console.WriteLine(i);
            }

        }

Insertion Sort :- In the Insertion Sort algorithm, we build a sorted list from the bottom of the array. We repeatedly insert the next element into the sorted part of the array by sliding it down to its proper position.

This will require as many exchanges as Bubble Sort, since only one inversion is removed per exchange. So Insertion Sort also requires O(N2)exchanges. On average Insertion Sort requires only half as many comparisons as Bubble Sort, since the average distance an element must move for random input is one-half the length of the sorted portion.

Public static void InsertionSort()
        {
            var inArray = new int[6] {1, 4, 6, 67, 78, 9};
            int temp;
            for (int i = 0; i < inArray.Length -1; i++)
            {
                for (int j = i + 1; j >0 ; j--)
                {
                    if (inArray[j- 1] > inArray[j])
                    {
                        temp = inArray[j -1];
                        inArray[j - 1] = inArray[j];
                        inArray[j] = temp;
                    }
                }
            }

            foreach (var array in inArray)
            {
                Console.WriteLine(array);
            }

        }


Selection Sort :-  Here Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.

public static void SelectionSort()
        {
            var inArray = new int[6] { 1, 4, 6, 67, 78, 9 };
            int temp;
            for (int i = 0; i < inArray.Length; i++)
            {
                for (int j = i + 1; j < inArray.Length; j++)
                {
                    if (inArray[i] > inArray[j])
                    {
                        temp = inArray[j];
                        inArray[j] = inArray[i];
                        inArray[i] = temp;
                    }
                }
            }

            foreach (var array in inArray)
            {
                Console.WriteLine(array);
            }

        }

Quick and Merge Sort example will be published in next blog.

No comments:

Post a Comment

Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...