Selection Sort

Selection Sort

  • What is Selection Sort?
    Selection Sort is a way to arrange a list of numbers (or items) in order. We do this by picking the smallest (or largest) number and placing it in its correct position, step by step.

  • How does it work?

    • Look at the whole list and find the smallest number.
    • Put that smallest number in the first position.
    • Now, ignore the first number (it’s already sorted) and look at the rest of the list. Find the next smallest number and put it in the second position.
    • Keep repeating this until the whole list is sorted.
  • Step-by-step process:

    • Find the smallest number in the list.
    • Swap it with the number in the first position.
    • Find the next smallest number and swap it with the second position.
    • Repeat until everything is sorted.
  • Example:
    List: [7, 3, 8, 4]

    • Step 1: Find the smallest number (3). Swap it with the first number (7).
      → [3, 7, 8, 4]
    • Step 2: Ignore the first number. Now find the smallest number in the rest (4). Swap it with the second number (7).
      → [3, 4, 8, 7]
    • Step 3: Ignore the first two numbers. Find the smallest number in the rest (7). Swap it with the third number (8).
      → [3, 4, 7, 8]

    Now the list is sorted!

  • Key idea:
    Find the smallest number, put it in the right spot, and repeat until the list is sorted.

  •  

    Comments

    Popular posts from this blog

    Programming Notes by Atul Kalukhe