public class SelectionSort {
// Function to perform Selection Sort
public static void selectionSort(int[] nums) {
int n = nums.length;
// Traverse through all array elements
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted array
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = nums[minIndex];
nums[minIndex] = nums[i];
nums[i] = temp;
}
}
// Main method to test the selection sort
public static void main(String[] args) {
int[] nums = {3, 1, 3, 2, 5, 4};
selectionSort(nums);
// Print the sorted array
for (int num : nums) {
System.out.print(num + " ");
}
}
}
### Explanation of Key Parts
- **Outer Loop**: Iterates through each element, considering 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 found with the first unsorted element.
## Complexity Analysis
### Time Complexity
- **Best, Average, and Worst Case**: O(n^2) due to the nested loops iterating through the array.
### Space Complexity
- **Space Complexity**: O(1) as it only uses a constant amount of extra space for variables.
## Edge Cases
### Potential Edge Cases
- **Empty Array**: The algorithm should handle an empty array without errors.
- **Single Element Array**: An array with a single element should remain unchanged.
- **Already Sorted Array**: The algorithm should still perform O(n^2) operations but the array remains unchanged.
- **Array with Duplicates**: The algorithm should correctly sort arrays with duplicate values.
### Testing Edge Cases
- **Empty Array**: `[]`
- **Single Element Array**: `[1]`
- **Already Sorted Array**: `[1, 2, 3, 4, 5]`
- **Array with Duplicates**: `[3, 1, 3, 2, 5, 4]`
## Testing
### Comprehensive Testing
To test the solution comprehensively, include a variety of test cases:
- Simple cases
- Edge cases
- Large arrays to observe performance
### Example Test Cases
java
public static void main(String[] args) {
int[][] testCases = {
{}, // Empty array
{1}, // Single element
{1, 2, 3, 4, 5}, // Already sorted
{5, 4, 3, 2, 1}, // Reverse sorted
{3, 1, 3, 2, 5, 4} // Array with duplicates
};
for (int[] testCase : testCases) {
selectionSort(testCase);
System.out.println(Arrays.toString(testCase));
}
}
## Thinking and Problem-Solving Tips
### Tips for Approaching Sorting Problems
- **Understand the Requirements**: Know the constraints and requirements of the problem.
- **Choose the Right Algorithm**: Select an appropriate sorting algorithm based on the problem constraints.
- **Optimize**: Look for ways to optimize the algorithm for better performance.
### Strategies to Develop Problem-Solving Skills
- **Practice Regularly**: Solve a variety of problems to improve your skills.
- **Study Algorithms**: Understand different algorithms and their use cases.
- **Analyze Solutions**: Review and analyze different solutions to understand their strengths and weaknesses.
## 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 a good starting point for learning about sorting algorithms. Understanding and implementing Selection Sort helps build a foundation for more advanced sorting techniques.
## Additional Resources
### Further Reading and Practice Problems
- [GeeksforGeeks - Selection Sort](https://www.geeksforgeeks.org/selection-sort/)
- [LeetCode - Sorting Problems](https://leetcode.com/tag/sorting/)
- [HackerRank - Sorting Challenges](https://www.hackerrank.com/domains/tutorials/10-days-of-javascript)
By practicing and exploring these resources, you can deepen your understanding of sorting algorithms and improve your problem-solving skills.
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