• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Selection Sort in Python
Next →
← Prev

Selection Sort in Python

By Dinesh Thakur

In this tutorial, we’ll talk about selection sort, but what’s wrong with bubble sort in python. The problem is in every iteration, first of all, we do multiple iterations because we have to make sure that you get assorted values, and at one point, you swap two values. The thing is, in each iteration, you do multiple swapping, so there’s nothing wrong in traversing from start to end. But swapping consumes the processing power, so we don’t want to do that. We don’t want to consume our CPU power and memory to do that, so swapping should be done only once, and that’s where selection sort comes into the picture.

We’ll be covering the following topics in this tutorial:

  • What is Selection Sort in Python
  • Python Program for Selection Sort

What is Selection Sort in Python

The Selection Sort algorithm sorts an array by recursively identifying the unsorted list’s minimal value and then replacing it with the first value. It is an in-place algorithm, so no additional lists are required. However, the primary sorting algorithm is still used in systems with limited memory.

Let’s see the visual representation of the algorithm

Selection Sort in pythonExplanation

So in such a case, we go from start to end, and we find either the min value or max value. When you depend upon different implementations in this example, we’ll go for the min value, so what you will do. You will go from start to end, and you will find the min value, so in each iteration, you will do only one thing, which is you will find the min value.

so let’s take this value so we have

[5 3 8 6 7 2]

We get the same values which we have taken for bubble sort, so what you will do is you will go from start to end, and you will find the min value, and we all know how to find a min value from the list.

So let’s we will assume that the min value is first value.

min value = 5

but we will start comparing the first value with each element so we compare 5 with 3

if 5 > 3 then

min value = 3

In this case, we know 3 is the min value, and you will compare with the next value and then the next value, and at the end, you will verify that 2 is a min value

min value = 2

So at the end of the first iteration, you’ve got the min value, which is two, and you will swap 5 to the right because we started with five, so now you will swap two with five, and your two goes at the first position.

[2 3 8 6 7 5]

Now from the remaining elements, you will do the same thing, and you will start with the first value, and you will go to the end value. After going from start to end, you got three as a min value, and if you can see in each iteration, we are doing swapping only one, so what you will swap the values with 3 becomes your second value, and now 2 and 3 becomes part of the sorted array.

[2 3 8 6 7 5]

and the remaining value becomes out of an unsorted array. After you will do the same thing with each iteration, you will start swapping the values right.

After the next iteration, you skip three, and then you will now escape the next mini value, which is five in this case, and the eight goes on at the end, you will get a sorted array.

[2 3 5 6 7 8]

So let’s try to implement that and this example. You already have a code here. You can see we have a sorting

Python Program for Selection Sort

def sort(nums):
    for i in range(5):
         minpos = i
         for j in range(i,6):
            if nums[j] < nums[minpos]:
               minpos = j
               temp = nums[i]
               nums[i] = nums[minpos]
               nums[minpos] = temp
nums = [5,3,8,6,7,2]
sort(nums)
print(nums)

Output:
Selection Sort in python Program

You’ll also like:

  1. Selection Sort in C
  2. Array C++ Selection Sort
  3. Bubble sort in Python
  4. What is Python? | Introduction to Python Programming
  5. Java – JComboBox Selection Change Listener Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW