- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selection Sort in Python Program
In this article, we will learn about the Selection sort and its implementation in Python 3.x. Or earlier.
In selection sort algorithm, an array is sorted by recursively finding the minimum element from the unsorted part and inserting it at the beginning. Two subarrays are formed during the execution of Selection sort on a given array.
- The subarray , which is already sorted.
- The subarray , which is unsorted.
During every iteration of selection sort, the minimum element from the unsorted subarray is popped and inserted into the sorted subarray.
Let’s see the visual representation of the algorithm −
Now let’s see the implementation of the algorithm −
Example
A = ['t','u','t','o','r','i','a','l'] for i in range(len(A)): min_= i for j in range(i+1, len(A)): if A[min_] > A[j]: min_ = j #swap A[i], A[min_] = A[min_], A[i] # main for i in range(len(A)): print(A[i])
Output
a i l o r t t u
Here we received output from the algorithm in ascending order. Min_ is the current value which is getting compared with all other values. The analysis parameters of the algorithm are listed below −
Time Complexity − O(n^2)
Auxiliary Space − O(1)
Here all the variables are declared in the global frame as shown in the image below Conclusion −
Conclusion
In this article, we learned about Selection sort and its implementation in Python 3.x. Or earlier.
- Related Articles
- Python Program for Selection Sort
- Selection Sort program in C#
- 8086 program for selection sort
- C Program for Selection Sort?
- Java program to implement selection sort
- C++ Program to Implement Selection Sort
- Selection Sort
- Selection sort in Java.
- 8085 Program to perform selection sort in ascending order
- 8085 Program to perform selection sort in descending order
- C++ program for Sorting Dates using Selection Sort
- 8085 Program to perform sorting using selection sort
- Program to perform sorting using selection sort in 8085 Microprocessor
- Selection Sort in Go Lang
- Recursive Selection Sort in C++
