
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)
Editorial
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. |
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