Welcome to TheCodingCollege.com, your one-stop destination for mastering coding and programming concepts. In this article, we will dive into Selection Sort, a simple yet important sorting algorithm in Data Structures and Algorithms (DSA).
What is Selection Sort?
Selection Sort is a comparison-based sorting algorithm that works by dividing the array into two parts:
- A sorted portion.
- An unsorted portion.
The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion and swaps it with the first element of the unsorted portion, gradually growing the sorted portion.
How Does Selection Sort Work?
The process involves finding the minimum element from the unsorted array and swapping it with the first element. This process is repeated for the remaining unsorted portion.
Steps in Selection Sort
- Start with the first element of the array.
- Find the smallest element in the unsorted portion of the array.
- Swap the smallest element with the first element of the unsorted portion.
- Move the boundary of the sorted portion one step forward.
- Repeat until the entire array is sorted.
Example of Selection Sort
Let’s sort the array [64, 25, 12, 22, 11]
in ascending order.
Initial Array: [64, 25, 12, 22, 11]
Pass 1:
- Find the smallest element in
[64, 25, 12, 22, 11]
: 11. - Swap 11 with 64.
- Array:
[11, 25, 12, 22, 64]
.
Pass 2:
- Find the smallest element in
[25, 12, 22, 64]
: 12. - Swap 12 with 25.
- Array:
[11, 12, 25, 22, 64]
.
Pass 3:
- Find the smallest element in
[25, 22, 64]
: 22. - Swap 22 with 25.
- Array:
[11, 12, 22, 25, 64]
.
Pass 4:
- Find the smallest element in
[25, 64]
: 25. - No swap needed.
Sorted Array: [11, 12, 22, 25, 64]
.
Selection Sort Code Implementation
Python Code:
def selection_sort(arr):
n = len(arr)
for i in range(n):
# Find the minimum element in the unsorted portion
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
# Swap the found minimum element with the first element
arr[i], arr[min_index] = arr[min_index], arr[i]
# Example usage
array = [64, 25, 12, 22, 11]
selection_sort(array)
print("Sorted Array:", array)
Output:
Sorted Array: [11, 12, 22, 25, 64]
Java Code:
class SelectionSort {
static void selectionSort(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void main(String args[]) {
int arr[] = {64, 25, 12, 22, 11};
selectionSort(arr);
System.out.print("Sorted Array: ");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
Time Complexity of Selection Sort
Case | Time Complexity |
---|---|
Best Case | O(n2)O(n^2) |
Average Case | O(n2)O(n^2) |
Worst Case | O(n2)O(n^2) |
Space Complexity: O(1)O(1) (In-place sorting).
Advantages and Disadvantages of Selection Sort
Advantages
- Simple to understand and implement.
- Works well for small datasets.
- In-place sorting with no additional memory required.
Disadvantages
- Inefficient for large datasets due to O(n2)O(n^2) time complexity.
- Performs unnecessary comparisons even if the array is already sorted.
Applications of Selection Sort
- Small Datasets: Suitable for scenarios with limited data.
- Learning Sorting Concepts: Helps beginners understand sorting algorithms.
- Memory-Constrained Environments: No extra space is required, making it ideal when memory usage is critical.
Comparison with Bubble Sort
Feature | Bubble Sort | Selection Sort |
---|---|---|
Comparisons | Adjacent elements | Finds minimum each pass |
Number of Swaps | More | Fewer |
Best Case Time Complexity | O(n)O(n) (Optimized) | O(n2)O(n^2) |
Stability | Stable | Unstable |
Conclusion
Selection Sort is an essential algorithm for understanding sorting concepts in DSA. Although it is not the most efficient for large datasets, its simplicity makes it a great choice for beginners. Start practicing Selection Sort and explore advanced sorting techniques on TheCodingCollege.com to level up your programming skills!