Skip to main content

Selection Sort

The least intelligent sorting algorithm, it typically has the worst performance of sorting routines, and learns nothing to improve its actions as it traverse the array:

Class

using System;

namespace Algorithms
{
    class SelectionSort
    {
        public SelectionSort()
        {
            int[] arrayToSort = { 11, 1, 22, 2, 33, 3, 44, 4, 55, 5, 66, 6, 7, 77 };
            Sort(arrayToSort);
            Console.WriteLine("End");
        }

        public int[] Sort(int[] arrayToSort)
        {
            int currentMin;
            int max = arrayToSort.Length - 1;
            for (int counter = 0; counter < max - 1; counter++)
            {
                currentMin = arrayToSort[counter];
                int newMinCounter = counter;
                for (int innerCounter = counter + 1; innerCounter < max; innerCounter++)
                { 
                    if (arrayToSort[innerCounter] < arrayToSort[newMinCounter])
                    {
                        //set new min
                        newMinCounter = innerCounter;
                    }
                }

                if (newMinCounter != counter)
                { 
                    //swap old value with new
                    arrayToSort[counter] = arrayToSort[newMinCounter];
                    arrayToSort[newMinCounter] = currentMin;
                }
            }

            return arrayToSort;
        }
    }
}

Comments

Popular posts from this blog

Review - TFS/VSTS - Great Product, Ideal for Small Development Shops

This is a report a short review I provided for G2 regarding TFS : What do you like best? If you use Visual Studio for development, TFS, or its online equivalent VSTS, you can have a fairly seamless end-to-end integration. Out of the box, it provides code management, testing, work hierarchy in agile formats, automated build, and deployment. What do you dislike? Branching and merging can be a bit painful, in that it needs to be planned, and is not natively part of the process. Code review also needs to be planned and only recently has it become part of the process. Recommendations to others considering the product My only concern regarding TFS and VSTS is that Microsoft itself recommends using Git. What business problems are you solving with the product? What benefits have you realized? In my current role, I've joined a shop that has application development as secondary to their role of desktop OS and app deployment/maintenance, so their code management practi...

Do Algorithms Make You a Better Developer?

Responding to a question on HashNode, Developers who practise algorithms are better at software development than people who just do development. Is it true? , I wrote the following: My feeling is that algorithms help make one a better programmer, but that is likely true of many coding concepts. I did not have algorithms as an undergraduate, so my knowledge is acquired through reading and practice, but after reading and applying Algorithm's in a Nutshell, I felt the quality of my work improved. That said, my development work increased more after understanding Design Patterns, or after consuming books on database design.  Since many types of knowledge improve developing and architecting abilities, one has to consider how it helps and to what degree. Algorithms are coding-in-the-small, often narrowly focused solutions, but which can have a great impact at scale. For many applications, a focus on algorithms would be overkill as data sets and requirements do not require it. In this ...

Heap Sort

Heap Sort is interesting because the use of the 'heapify' method that creates a binary tree as a flat array. First, a binary tree, also known as a heap , is created, and then the same function is used to sort the elements. In a heap, the first node in the array at zero (0) is the top node of the binary tree. The next two (2) items are the two (2) subnodes of the top node, and so on.  For each node at a position (positionIndex), its 2 subnodes are in the following positions: int left = 2 * positionIndex + 1; int right = 2 *  positionIndex + 2; The CommonMethods.Swap() function is a reusable class , since swapping values by position is a common action during these example sorts. Class using System; namespace Algorithms {     class HeapSort     {         public int[]  Sort()         {             //creates array             int[] arrayTo...