1

What type of sorting algorithm is this? It goes through each index, then gets the minimum value in the rest of the array and swaps.

private void Sort(int[] myArray)
{
        for (int i = 0; i < myArray.Length; i ++)
        {
            var minValue = myArray[i];
            var minIndex = i;

            for(int j = i +1; j< myArray.Length; j++)
            {
                if (myArray[j] < minValue)
                {
                    minIndex = j;
                    minValue = myArray[j];
                }
            }

            var temp = myArray[i];
            myArray[i] = myArray[minIndex];
            myArray[minIndex] = temp;
        }
}
3
  • 2
    For what it's worth, if I do a Google search for "sorting algorithm goes through each index, then gets the minimum value in the rest of the array and swaps", the first link (excluding this question) tells me it's selection sort. Commented Nov 21, 2018 at 7:54
  • @Dukeling You'd be surprised (or perhaps not, by now) how often a Google search for the title question or the first line reveals the answer. Commented Nov 21, 2018 at 14:56
  • @JimMischel I know that all too well. In this case I thought it useful to point it out since it might not be the type of thing someone would typically expect to get an answer through Google for (it's not one of the many "how do I do X" questions). Commented Nov 21, 2018 at 17:42

1 Answer 1

2

It's called Selection sort. As you have already noticed, it selects the minimum element in the remaining list, swaps it in order and repeats until no more elements left.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.