Program for Selection sort using Python

by | Jun 26, 2021 | Python Programs

Home » Python » Python Programs » Program for Selection sort using Python

Introduction

The task is to perform selection sort on given elements.

Program for Selection sort using Python

Program

import sys
ip_arr = [1, 100, 6, 3]
print("The input array is: ", ip_arr)
for i in range(len(ip_arr)):
    idx = i
    for j in range(i +1 , len(ip_arr)):
        if ip_arr[idx] > ip_arr[j]:
            idx = j
# Swapping elements
    ip_arr[i], ip_arr[idx] = ip_arr[idx], ip_arr[i]
print("The sorted array is :")
for i in range(len(ip_arr)):
    print("%d" %ip_arr[i])

Output

Program for Selection sort using Python Output

Explanation

The selection sort maintain two arrays from the given array. One is sorted array and another one is unsorted array. The minimum element is search from unsorted array and placed at the correct positioned in sorted array.

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author