0%
Posted inComputer Science

# Simple selection sort in python ###### 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)
```