Selection Sorting in Data Structure

Selection Sorting in data structure refers to find the largest ( or smallest ) element in an array repeatedly and move that element to its final position.

Advantage of Selection Sorting

  • It is a in place algorithm
  • Does not require to much space to sort.

Disadvantage of Selection Sort

  • Difficult to sort large size of data.
  • It is not scalable.

Selection Sort C++

The program of selection sort in c++ is as follows:

//To perform selection sorting//
#include<iostream>
using namespace std;
void select(int ar[10])
{
int min,locn,temp;
for(int i=0;i<10;i++)
{
min=ar[i];
locn=i;
for(int j=i+1;j<10;j++)
{
if(ar[j]<min)
{
min=ar[j];
locn=j;
}
}
temp=ar[i];
ar[i]=ar[locn];
ar[locn]=temp;
}
for(int i=0;i<10;i++)
{
cout<<ar[i]<<endl;
}
}
int main()
{
int ar1[10];
cout<<"enter 10 numbers in an array for sort\n";
for(int i=0;i<10;i++)
{
cin>>ar1[i];
}
cout<<"array after selection sort"<<endl;
select(ar1);
return 0;
}

Output of the program selection sort in c++ is as follows:

Selection Sorting

Selection Sort C

The program of selection sort in c is as follows:

#include<stdio.h>

void select(int ar[10])
{
int min,locn,temp;
for(int i=0;i<10;i++)
{
min=ar[i];
locn=i;
for(int j=i+1;j<10;j++)
{
if(ar[j]<min)
{
min=ar[j];
locn=j;
}
}
temp=ar[i];
ar[i]=ar[locn];
ar[locn]=temp;
}
for(int i=0;i<10;i++)
{
printf("%d\n",ar[i]);
}
}
int main()
{
int ar1[10];
printf("enter 10 numbers in an array for sort\n");
for(int i=0;i<10;i++)
{
scanf("%d",&ar1[i]);
}
printf("array after selection sort\n");
select(ar1);
return 0;
}

Output of the program selection sort in c is as follows:

Selection Sorting

Selection Sort in Python with User Input

The program of selection sort in python with user input is as follows:

def selectionSort(array, size):
    
    for ind in range(size):
        min_index = ind
 
        for j in range(ind + 1, size):
            # select the minimum element in every iteration
            if array[j] < array[min_index]:
                min_index = j
         # swapping the elements to sort the array
        (array[ind], array[min_index]) = (array[min_index], array[ind])

arr = input('Enter the 10 numbers in the list: ')
arr = arr.split()
arr = [int(x) for x in arr]
size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection sort is:')
print(arr)

Output of the program selection sort in python with user input is as follows:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments