Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array.
The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays.
One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations.
Another advantage of selection sort is that it is a stable sorting algorithm, meaning that it preserves the relative order of elements with the same value. This means that if there are multiple elements with the same value in the array, their order will not change after sorting.
In conclusion, selection sort is a simple sorting algorithm that is easy to understand and implement, but it is not a good choice for large arrays due to its poor time complexity. However, it has some advantages such as making fewer swaps and being a stable sorting algorithm.
ALGORITHM:
- Start by dividing the array into two parts: the sorted portion, which starts as an empty list, and the unsorted portion, which is the entire array.
- Find the minimum element from the unsorted portion of the array.
- Swap the minimum element found in step 2 with the first element of the unsorted portion of the array.
- Move the boundary between the sorted and unsorted portions one element to the right.
- Repeat steps 2 to 4 until all elements of the array have been moved to the sorted portion.
PSEUDO CODE:
PYTHON CODE:
JAVA CODE:
In this implementation, the arr array is passed to the selectionSort method, which sorts the array in place. The outer loop iterates n-1 times, and for each iteration, the inner loop finds the index of the minimum element in the unsorted portion of the list. If the minimum element is not already in its correct position, it is swapped with the first element of the unsorted portion. The boundary between the sorted and unsorted portions of the list is then moved one element to the right. The inner loop repeats until all elements have been moved to the sorted portion of the list.
C CODE:
In this implementation, the arr array and its length n are passed to the selection_sort function, which sorts the array in place. The outer loop iterates n-1 times, and for each iteration, the inner loop finds the index of the minimum element in the unsorted portion of the list. If the minimum element is not already in its correct position, it is swapped with the first element of the unsorted portion. The boundary between the sorted and unsorted portions of the list is then moved one element to the right. The inner loop repeats until all elements have been moved to the sorted portion of the list.
C++ CODE:
JAVASCRIPT CODE:
In this implementation, the arr array is passed to the selection Sort function, which sorts the array in place. The outer loop iterates n-1 times, and for each iteration, the inner loop finds the index of the minimum element in the unsorted portion of the list. If the minimum element is not already in its correct position, it is swapped with the first element of the unsorted portion. The boundary between the sorted and unsorted portions of the list is then moved one element to the right. The inner loop repeats until all elements have been moved to the sorted portion of the list.
Thanks for explaining beautifully, sir.
ReplyDeleteGlad you liked this one
Delete