Selection sort
Selection Sort Selection sort is a simple yet effective sorting algorithm that works by repeatedly finding the minimum (or maximum) element in the unsorted...
Selection Sort Selection sort is a simple yet effective sorting algorithm that works by repeatedly finding the minimum (or maximum) element in the unsorted...
Selection Sort
Selection sort is a simple yet effective sorting algorithm that works by repeatedly finding the minimum (or maximum) element in the unsorted portion of the list and moving it to the end. This process continues until the entire list is sorted.
Algorithm:
Start at the first element in the unsorted portion of the list.
Find the minimum (or maximum) element in the unsorted portion.
Move the minimum (or maximum) element to the end of the sorted portion of the list.
Repeat steps 1-3 until the entire list is sorted.
Example:
Unsorted List:
[8, 4, 9, 7, 2]
Sorted List:
[2, 4, 8, 9, 7]
Time Complexity:
The time complexity of selection sort is O(n^2), where n is the length of the list. This is because the algorithm iterates over the list n times, once for each element.
Advantages:
Simple and easy to implement.
Works for any type of data.
Has a time complexity that is nearly optimal.
Disadvantages:
The worst-case performance is O(n^2), which can occur if the list contains a single minimum or maximum element.
Selection sort is not as efficient for small lists.
It is not suitable for data with a high number of elements or a large data set