Blog
15 BEST Free DVD Player for Windows (2021)
DVD players are software that allows you to view videos on PC using a DVD-ROM drive. You can use...
SELECTION SORT is a comparison sorting algorithm that is used to sort a random list of items in ascending order. The comparison does not require a lot of extra space. It only requires one extra memory space for the temporal variable.
This is known as in-place sorting. The selection sort has a time complexity of O(n2) where n is the total number of items in the list. The time complexity measures the number of iterations required to sort the list. The list is divided into two partitions: The first list contains sorted items, while the second list contains unsorted items.
By default, the sorted list is empty, and the unsorted list contains all the elements. The unsorted list is then scanned for the minimum value, which is then placed in the sorted list. This process is repeated until all the values have been compared and sorted.
In this Algorithm tutorial, you will learn:
The first item in the unsorted partition is compared with all the values to the right-hand side to check if it is the minimum value. If it is not the minimum value, then its position is swapped with the minimum value.
A list of elements that are in random order needs to be sorted in ascending order. Consider the following list as an example.
[21,6,9,33,3]
The above list should be sorted to produce the following results
[3,6,9,21,33]
Step 1) Get the value of n which is the total size of the array
Step 2) Partition the list into sorted and unsorted sections. The sorted section is initially empty while the unsorted section contains the entire list
Step 3) Pick the minimum value from the unpartitioned section and placed it into the sorted section.
Step 4) Repeat the process (n – 1) times until all of the elements in the list have been sorted.
Given a list of five elements, the following images illustrate how the selection sort algorithm iterates through the values when sorting them.
The following image shows the unsorted list
Step 1)
The first value 21 is compared with the rest of the values to check if it is the minimum value.
3 is the minimum value, so the positions of 21 and 3 are swapped. The values with a green background represent the sorted partition of the list.
Step 2)
The value 6 which is the first element in the unsorted partition is compared with the rest of the values to find out if a lower value exists
The value 6 is the minimum value, so it maintains its position.
Step 3)
The first element of the unsorted list with the value of 9 is compared with the rest of the values to check if it is the minimum value.
The value 9 is the minimum value, so it maintains its position in the sorted partition.
Step 4)
The value 33 is compared with the rest of the values.
The value 21 is lower than 33, so the positions are swapped to produce the above new list.
Step 5)
We only have one value left in the unpartitioned list. Therefore, it is already sorted.
The final list is like the one shown in the above image.
The following code shows the selection sort implementation using Python 3
def selectionSort( itemsList ):
n = len( itemsList )
for i in range( n - 1 ):
minValueIndex = i
for j in range( i + 1, n ):
if itemsList[j] < itemsList[minValueIndex] :
minValueIndex = j
if minValueIndex != i :
temp = itemsList[i]
itemsList[i] = itemsList[minValueIndex]
itemsList[minValueIndex] = temp
return itemsList
el = [21,6,9,33,3]
print(selectionSort(el))
Run the above code produces the following results
[3, 6, 9, 21, 33]
The explanation for the code is as follows
Here is Code explanation:
The sort complexity is used to express the number of execution times it takes to sort the list. The implementation has two loops.
The outer loop which picks the values one by one from the list is executed n times where n is the total number of values in the list.
The inner loop, which compares the value from the outer loop with the rest of the values, is also executed n times where n is the total number of elements in the list.
Therefore, the number of executions is (n * n), which can also be expressed as O(n2).
The selection sort has three categories of complexity namely;
The selection sort has a space complexity of O(1) as it requires one temporal variable used for swapping values.
The selection sort is best used when you want to:
The following are the advantages of the selection sort
The following are the disadvantages of the selection sort.
DVD players are software that allows you to view videos on PC using a DVD-ROM drive. You can use...
What is SAS? SAS stands for S tatistical A nalysis S oftware which is used for Data Analytics. It helps...
What is Concurrency or Single Core? In Operating Systems, concurrency is defined as the ability of a...
What is R List? R List is an object in R programming which includes matrices, vectors, data...
What is ServiceNow? ServiceNow is a cloud-based software platform for IT Service Management (ITSM) which...
M4V to MP4 converter is an application that can convert M4V (iTunes Video) files to MP4 (MPEG-4...