Tutorialspoint
Problem
Solution
Submissions

Selection Sort Algorithm

Certification: Intermediate Level Accuracy: 100% Submissions: 1 Points: 15

Write a Python program that implements the Selection Sort algorithm to sort a list of integers in ascending order. Selection Sort works by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning.

Example 1
  • Input: [64, 25, 12, 22, 11]
  • Output: [11, 12, 22, 25, 64]
  • Explanation:
    • Step 1: Take the input array [64, 25, 12, 22, 11].
    • Step 2: Find the minimum element in the unsorted array (11) and swap it with the first element (64).
    • Step 3: After first pass: [11, 25, 12, 22, 64].
    • Step 4: Find the minimum element in the remaining unsorted array [25, 12, 22, 64] (which is 12) and swap it with the first element of the unsorted part (25).
    • Step 5: After second pass: [11, 12, 25, 22, 64].
    • Step 6: Continue this process for all elements.
    • Step 7: Return the sorted array [11, 12, 22, 25, 64].
Example 2
  • Input: [5, 1, 4, 2, 8]
  • Output: [1, 2, 4, 5, 8]
  • Explanation:
    • Step 1: Take the input array [5, 1, 4, 2, 8].
    • Step 2: Find the minimum element in the unsorted array (1) and swap it with the first element (5).
    • Step 3: After first pass: [1, 5, 4, 2, 8].
    • Step 4: Find the minimum element in the remaining unsorted array [5, 4, 2, 8] (which is 2) and swap it with the first element of the unsorted part (5).
    • Step 5: After second pass: [1, 2, 4, 5, 8].
    • Step 6: Continue this process for all elements.
    • Step 7: Return the sorted array [1, 2, 4, 5, 8].
Constraints
  • 1 ≤ len(arr) ≤ 10^3
  • -10^6 ≤ arr[i] ≤ 10^6
  • Time Complexity: O(n²), where n is the length of the array
  • Space Complexity: O(1)
ArraysNumberControl StructuresEYSnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Divide the array into two parts: sorted and unsorted
  • Find the minimum element in the unsorted part
  • Swap it with the first element of the unsorted part
  • Extend the sorted part by including the newly placed element
  • Repeat until the entire array is sorted
  • The outer loop runs n-1 times, where n is the length of the array
  • The inner loop finds the minimum element in the unsorted part

Steps to solve by this approach:

 Step 1: Create a copy of the input array to avoid modifying the original.
 Step 2: Iterate through each position i from 0 to n-1.
 Step 3: For each position i, find the minimum element in the unsorted portion (i to n-1).
 Step 4: Maintain the index of the minimum element found (min_idx).
 Step 5: After finding the minimum, swap it with the element at position i.
 Step 6: Continue this process until the entire array is sorted.
 Step 7: Return the sorted array.

Submitted Code :