Selection Sort in Python

Selection sort is one of the simplest sorting algorithms. This sorting algorithm repeatedly selects the smallest element from the unsorted portion of the list and places it at the beginning of the sorted portion of the list. The algorithm then repeats this process for the remaining unsorted portion until the entire list is sorted.

How Selection Sort Works?

The main idea behind selection sort is that we select the smallest element and swap it with the first element of the unsorted part of the list. This process is repeated until the entire list is sorted. Here are the steps:

1. We start from the first element of the list and assume it to be the smallest element.
2. We then compare this element with the rest of the elements in the list.
3. If we find an element that is smaller than the current smallest element, we swap it with the current smallest element.
4. We repeat steps 2 and 3 until we reach the end of the list.
5. We then move to the second element of the list and repeat the above steps again until the entire list is sorted.

Implementing Selection Sort in Python

Let’s take a look at the implementation of selection sort in Python:

def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

return arr

The function takes an array as an argument and returns the sorted array. We first calculate the length of the array and then iterate over each element of the array using a for loop. We assume the first element to be the smallest and then find the smallest element in the unsorted part of the list by iterating over the rest of the elements using another for loop.

After finding the smallest element, we then swap it with the first element of the unsorted part of the list. This way, we sort the array by repeatedly selecting the smallest element and placing it at the beginning of the sorted part of the list.

Pros and Cons of Selection Sort

Pros

– Simple implementation
– Works well for small lists
– In-place sorting algorithm (modifies the original array)

Cons

– Inefficient for large lists (O(N^2) complexity)
– Not suitable for partially sorted lists
– Does not take advantage of pre-existing sorted order

Selection Sort FAQs

What is the time complexity of selection sort?

The time complexity of selection sort is O(N^2). This means that the time taken to sort the list grows exponentially as the size of the list increases.

Is selection sort stable?

No, selection sort is not stable. A stable sorting algorithm maintains the order of equal elements in the original list, but selection sort does not guarantee this.

How does selection sort compare to other sorting algorithms?

Selection sort is one of the simplest sorting algorithms and has an average case time complexity of O(N^2). It is not the most efficient algorithm for sorting large lists, but it can be useful for small lists or for educational purposes. Other sorting algorithms like quicksort and mergesort have better time complexity and are more suitable for sorting large lists.

Can selection sort be used for sorting strings?

Yes, selection sort can be used for sorting strings as long as we can compare the strings. The algorithm works by comparing the elements of the list and swapping them if necessary, so it can be used for any data type that has a defined comparison operator.

Conclusion

Selection sort is a simple but inefficient sorting algorithm. It can be useful for small lists or for educational purposes, but it is not suitable for large lists or partially sorted lists. However, understanding the process of selection sort can help when learning more complex sorting algorithms.

Facebook
Twitter
LinkedIn
Pinterest

Related posts