Thumbnail article
Simple selection sort in python
One of a simple sorting algorithm is selection sort. This sorting method is based on in-place comparison and divides the list into two parts. The sorted part on the left and the unsorted part on the right. The sorted section is initially empty, and the unsorted part initially contains the complete list. The unsorted array’s smallest element is chosen and swapped with the leftmost element, resulting in the left becoming a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.
import sys
data = [4, 3, 5, 1, 2]
print("init data :", data)
for i in range(len(data)):
    # Find the minimum element in remaining unsorted array
    min_index = i
    for j in range(i+1, len(data)):
        if data[min_index] > data[j]:
            min_index = j
    # Swap the found minimum element with the first element       
    data[i], data[min_index] = data[min_index], data[i]
print("data after sorting :", data)
Disadvantages and advantages of Selection Sort
Every algorithm have advantages and disadvantages, such as faster but not practical or longer but the process is very practical. Likewise with the selection sort algorithm which has advantages and disadvantages in the process to sort the data.The advantages using selection sort algorithm are as follows :
  • This algorithm is very compact and easy to implement. You can already sort the number by using a simple example code above
  • The main advantage of the selection sort is that it performs well on a small list
  • Its performance is easily influenced by the initial ordering of the items before the sorting process
  • Easy to determine maximum or minimum data
  • The complexity of selection sort is relatively smaller
The disadvantages using the selection sort algorithm are as follows :
  • The primary disadvantage of the selection sort is it has a poor efficiency when dealing with a huge list of items
  • It should be avoided when the data more than 1000 tables, because it will cause higher complexity and less practical

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *