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

Popular posts from this blog

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity