Selection Sort Pseudocode

There are many different ways to sort the cards. Here's a simple one, called selection sort, possibly similar to how you sorted the cards above:

Find the smallest card. Swap it with the first card.

Find the second-smallest card. Swap it with the second card.

Find the third-smallest card. Swap it with the third card.

Repeat finding the next-smallest card, and swapping it into the correct position until the array is sorted.

This algorithm is called selection sort because it repeatedly selects the next-smallest element and swaps it into place.

You can see the algorithm for yourself below. Start by using "Step" to see each step of the algorithm, and then try "Automatic" once you understand it to see the steps all the way through.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy