Skip to main content

Selection Sort


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:

The algorithm works as follows:
  1. 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.
  2. Find the minimum element from the unsorted portion of the array.
  3. Swap the minimum element found in step 2 with the first element of the unsorted portion of the array.
  4. Move the boundary between the sorted and unsorted portions one element to the right.
  5. Repeat steps 2 to 4 until all elements of the array have been moved to the sorted portion.

PSEUDO CODE:

Here is the pseudo code for the selection sort algorithm:


In the above pseudo code, A is the list of elements to be sorted, n is the number of elements in the list, i and j are loop variables, and minIndex is the index of the minimum element found in the inner loop. 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.

PYTHON CODE:

Here is an implementation of the selection sort algorithm in Python:


In this implementation, arr is the list of elements to be sorted. 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.

JAVA CODE:

Here is the Java code for the Insertion Sort algorithm:


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:

Here is an implementation of the selection sort algorithm in C:


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:

Here is an implementation of the selection sort algorithm in C++:


In this implementation, the arr vector is passed to the selection sort function, which sorts the vector 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.

JAVASCRIPT CODE:

Here is an implementation of the selection sort algorithm in JavaScript:


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.

Comments

Post a Comment