// Function to perform Selection Sort
function selectionSort(nums) {
// Get the length of the array
const n = nums.length;
// Loop over the entire array
for (let i = 0; i < n - 1; i++) {
// Assume the first unsorted element is the minimum
let minIndex = i;
// Find the minimum element in the unsorted portion
for (let j = i + 1; j < n; j++) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first unsorted element
if (minIndex !== i) {
[nums[i], nums[minIndex]] = [nums[minIndex], nums[i]];
}
}
// Return the sorted array
return nums;
}
// Example usage
const nums = [3, 1, 3, 2, 5, 4];
console.log(selectionSort(nums)); // Output: [1, 2, 3, 3, 4, 5]
### Explanation of Key Parts
- **Outer Loop**: Iterates over each element, treating it as the boundary between sorted and unsorted portions.
- **Inner Loop**: Finds the minimum element in the unsorted portion.
- **Swap Operation**: Swaps the minimum element with the first unsorted element to move it to its correct position.
## Complexity Analysis
### Time Complexity
- **Best, Average, and Worst Case**: O(n^2) because we have nested loops iterating over the array.
### Space Complexity
- **Space Complexity**: O(1) because we are sorting the array in place and not using any extra space.
## Edge Cases
### Identifying Edge Cases
- **Empty Array**: Should return an empty array.
- **Single Element Array**: Should return the same array.
- **Already Sorted Array**: Should handle efficiently without unnecessary swaps.
- **Array with Duplicates**: Should sort correctly while handling duplicates.
### Examples of Edge Cases
- **Empty Array**: `[]` -> `[]`
- **Single Element**: `[1]` -> `[1]`
- **Already Sorted**: `[1, 2, 3, 4, 5]` -> `[1, 2, 3, 4, 5]`
- **Duplicates**: `[3, 3, 2, 1, 2]` -> `[1, 2, 2, 3, 3]`
## Testing
### Comprehensive Testing
To ensure the solution works correctly, test with a variety of cases:
- **Simple Cases**: Small arrays with distinct elements.
- **Complex Cases**: Larger arrays, arrays with duplicates, and arrays in reverse order.
- **Edge Cases**: Empty arrays, single-element arrays, and already sorted arrays.
### Example Test Cases
// Test cases
console.log(selectionSort([])); // Output: []
console.log(selectionSort([1])); // Output: [1]
console.log(selectionSort([1, 2, 3, 4, 5])); // Output: [1, 2, 3, 4, 5]
console.log(selectionSort([5, 4, 3, 2, 1])); // Output: [1, 2, 3, 4, 5]
console.log(selectionSort([3, 3, 2, 1, 2])); // Output: [1, 2, 2, 3, 3]
## Thinking and Problem-Solving Tips
### Tips for Approach
- **Understand the Problem**: Break down the problem into smaller parts.
- **Start Simple**: Begin with a naive solution and then optimize.
- **Practice**: Solve similar problems to improve problem-solving skills.
### Strategies to Develop Skills
- **Study Algorithms**: Learn different sorting algorithms and their use cases.
- **Practice Regularly**: Use coding challenge platforms to practice.
- **Analyze Solutions**: Understand the time and space complexity of different solutions.
## Conclusion
Selection Sort is a fundamental sorting algorithm that is easy to understand and implement. While it is not suitable for large datasets due to its O(n^2) time complexity, it is useful for educational purposes and small arrays. Understanding and implementing Selection Sort helps build a strong foundation in sorting algorithms.
## Additional Resources
### Further Reading and Practice Problems
- [Sorting Algorithms - GeeksforGeeks](https://www.geeksforgeeks.org/sorting-algorithms/)
- [Selection Sort - Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)
- [LeetCode Sorting Problems](https://leetcode.com/tag/sorting/)
### Tutorials and Documentation
- [JavaScript Array Methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
- [Sorting Algorithms Visualization](https://visualgo.net/en/sorting)
By understanding and practicing Selection Sort, you can enhance your problem-solving skills and prepare for more complex algorithms and data structures. Happy coding!
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE