The selection sort is also known as push down sort. This sort consists of a selection phase in which repeatedly identifies the smallest remaining unsorted element and puts it at the end of the sorted portion of the array.
ALGORITHM FOR SELECTION SORT:-
Let arr be an array of n elements.
Step 1: Find the position i of the smallest element in the list of n element arr [0],arr[1],. . . ,arr[n-1] and then
interchange arr[i] with arr[0]. Then arr[0] is sorted.
Step 2: Find the
position i of the smallest in the sub
list of n-1 element arr[1],. . . ,arr[n-1]. Then
interchange arr[i] with arr[1]. Then arr[0],arr[1]
are sorted.
-
-AZ
-
Step n: Find the position i of the smallest of the element arr[n-2],arr[n-1] and then
interchange arr[i] with arr[n-2].Then arr[0],arr[1], . . .,arr[n-1] are sorted.
Selection Sort Program in C
Solution In C
Code:
#include<stdio.h>
int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Elements are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
printf("\nAfter Sorting Array Elements are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Elements are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
printf("\nAfter Sorting Array Elements are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Input/Output:
Enter the size of the array:5
Enter the element of the array:
4
3
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
Selection Sort Program in C plus plus
Solution In C++
Code:
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Elements are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
cout<<"\nAfter Sorting Array Elements are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Elements are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
cout<<"\nAfter Sorting Array Elements are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
Input/Output:
Enter the size of the array:4
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Selection Sort Program in Java
Code:
import java.util.Scanner;
public class Selection_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Elements are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
System.out.print("\nAfter Sorting Array Elements are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
public class Selection_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Elements are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out, in, min;
for(out=0; out<size-1; out++)
{
min = out;
for(in=out+1; in<size; in++){
if(arr[in] < arr[min] ) {
min = in;
int temp=arr[out];
arr[out]=arr[min];
arr[min]=temp;
} }}
System.out.print("\nAfter Sorting Array Elements are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
Input/Output:
Enter the size of the array:4
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Selection Sort Program in python using list
Solution In Python
size=int(input("Enter the size of the array:"));
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Elements are: ",arr)
for out in range(0,size-1):
min = out
for inn in range(out+1, size):
if arr[inn] < arr[min]:
min = inn
temp=arr[out]
arr[out]=arr[min]
arr[min]=temp
print("\nAfter Sorting Array Elements are: ",arr)
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Elements are: ",arr)
for out in range(0,size-1):
min = out
for inn in range(out+1, size):
if arr[inn] < arr[min]:
min = inn
temp=arr[out]
arr[out]=arr[min]
arr[min]=temp
print("\nAfter Sorting Array Elements are: ",arr)
Enter the size of the array:5
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
0 Comments
Please do not Enter any spam link in the comment box