- The Process of selecting the minimum element in each pass and putting it in the appropriate position is called Selection Sort.
- As the passes going on, the first part will be sorted and the rest will be unsorted.
- Finally, the whole array or list will be sorted.
- Selection sort is an in-place comparison Sorting Algorithm.
- The time complexity of the Selection sort is O(n^2).
- The Space Complexity Of Selection Sort is O(1).
Selection Sort Implementation In C Language:
/* SelectionSort Implementation in C Language */
#include<stdio.h>
void SelectionSort(int A[], int n){
for(int i=0;i<n-1;i++){ // from o th to n-1 th index
int minEle = i; // let min element index = i, for every pass
for(int j=i+1;j<n;j++){ // from i+1 th index to n th index
if(A[j]<A[minEle]){ // if any element is greater than minimum element
minEle=j; // minimum element = j
}
}
int temp = A[i]; // swappning
A[i] = A[minEle];
A[minEle] = temp;
}
}
int main()
{
int A[] = {2,1,6,2,2,2,3,5,6,23};
int n = 10;
SelectionSort(A,n);
for(int i=0;i<n;i++){
printf("%d ",A[i]);
}
}
// Time Complexity: O(n^2)
// Space Complexity: O(1)
Output:
Selection Sort C Output |
Selection Sort Implementation In C++:
/* Selection Sort Implementation In C++ */
#include<iostream>
using namespace std;
void SelectionSort(int arr[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
int MinEle = i;
for(j=i+1; j<n; j++){
if(arr[j]<arr[MinEle]){
MinEle = j;
}
}
temp = arr[i];
arr[i] = arr[MinEle];
arr[MinEle] = temp;
}
}
int main(){
int A[] = {5,2,76,32,6,1,7};
SelectionSort(A,7);
for(int i=0;i<7;i++){
cout<<A[i]<<" ";
}
}
// Time Complexity of Selection Sort: O(n^2)
// Space Complexity: O(1)
Output:
Selection Sort C++ Output |
Selection Sort Implementation in Python:
# Selection Sort Implementation In Python
A = [34,5,12,45,4,434,65]
def SelectionSort(A):
for i in range(len(A)-1): #from 1st index to n-1 index
MinEle = i # Let Minimum element index = i
for j in range(i+1,len(A)): # From i+1 th element to n
if A[j]<A[MinEle]: # comparing with all the elements from i+1 to n
MinEle = j # if any element between i+1 to n elements is lessthan minimum element, minimum element index = i
A[i],A[MinEle] = A[MinEle],A[i] # swapping the numbers
return A
print(SelectionSort(A))
Output:
Selection Sort Python Output |
Selection Sort Implementation In Java:
/* Selection Sort Implementation In Java */
public class Main {
public static void ssrt(int A[], int n) {
for (int i = 0; i < n - 1; i++) { // from 0 to n-1 elements
int MinEle = i; // let minimum element index = i
for (int j = i + 1; j < n; j++) { // from i+1 to n elements
if (A[j] < A[MinEle]) { // Comparing the minimum element with all the elements from i+1 to n
MinEle = j; // minimum element index = index of element less than minimum element in range i+1 to n
}
}
int temp = A[i]; //Swapping minimum element and i th element
A[i] = A[MinEle];
A[MinEle] = temp;
}
}
public static void main(String[] args) {
int A[] = {67,5,67,2,45,34,657};
ssrt(A,A.length);
for(int i=0;i<A.length;i++){
System.out.println(A[i]);
}
}
}
// Time Complexity: O(n^2)
// Space Complexity: O(n)
Output:
Selection Sort Java Output
Conclusion:
The above all are the implementations of the Selection Sort Algorithm in C, C++, Python and Java. If you want to contribute feel free to comment your thoughts or code.
No comments:
Post a Comment