Menu
Coddy logo textTech

Pseudo Code

Lesson 4 of 9 in Coddy's Selection Sort - DSA Series course.

for i = 0 to n-1:
   minIndex = i
   for j = i+1 to n-1:
      if array[j] < array[minIndex]:
         minIndex = j
   swap array[i] and array[minIndex]

Let's connect the pseudocode to the idea:

  • i marks the boundary between the sorted part and the unsorted part. Everything before i is already sorted.
  • minIndex remembers the position of the smallest element found so far in the unsorted part.
  • The inner loop over j scans the unsorted part to find the true smallest element.
  • After the inner loop, we swap the smallest element into position i, growing the sorted part by one.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Selection Sort - DSA Series