In this article, you'll be given a beginner's introduction to Selection Sort through multiple instructional videos and supplementary exercises. They should be completed in order, and it's recommended that you pause and re-watch any parts that you have trouble following.

1. How Selection Sort Works

We’re going to learn about the selection sort algorithm and how it works. Selection sort is one of the most simple sorting algorithms. It’s used to sort a list of numbers in ascending order, starting with the smallest numbers on the left, moving up to the largest numbers on the right. Typically this is demonstrated using a list of numbers, but we’re going to apply the selection sort algorithm to a list of cubes in Unity3D.

Watch video

Exercise:
Make sure that you understand how the selection sort algorithm operates before moving on. Use this video and any external resources you can find to support you. Search for other popular sorting algorithms and how they work too, e.g. Bubble sort.

2. Selection Sort in C#

Next, we’re going to have a quick look at how implement the algorithm using C# in Unity. We'll do this in a single script and use it to sort an unordered list of numbers and then print them to our Unity console.

Watch video

Exercise:
Try passing in arrays of different size with different numbers to verify your Selection Sort implementation is functioning as intended.

3. Random Initialisation of GameObjects

In order to sort a series of cubes by height, we're first going to create a function that randomly generates cubes of different height.

Watch video

Exercise:
Try experimenting with different numbers of cubes and maximum heights. You could also try this with float point numbers instead of integers to make it more interesting.

4. Visually Sorting GameObjects

Finally, we’re going to learn how to sort these randomly generated cubes according to their height, using the selection sort algorithm. We'll continue using our project from the last exercise which already contains the code for the random cube initialisation.

Watch video

Exercise:
Try experimenting with different numbers of cubes and maximum heights. You could also try this with float point numbers instead of integers to make it more interesting.

Extra 1. Bubble Sort

Now try creating another function that implements the Bubble Sort instead. You can take this further by creating a user interface that allows the user to select either "Bubble Sort" or "Selection Sort".

Extra 2. Insertion Sort

Now try creating another function that implements the Insertion Sort instead. You can take this further by creating a user interface that allows the user to select either "Insertion Sort", "Bubble Sort", or "Selection Sort".

Extra 3. Create your first game p.2

You should try to reproduce the game in the video whilst you’re watching it. You’ll learn how to create a game including the following:

  • Importing 3D models
  • Character movement
  • Spawning and moving collectables
  • Spawning and moving hazards
  • Unity GUI
  • Health System
  • Scoring System (including high-score)
  • Persistent high-scores (e.g. restarting the game keeps the previous high score data).

This is a different style of video as it was streamed live.

Watch video


Leave a Reply

Close Menu