You are on page 1of 7

ASSIGNMENT 1

SELECTION SORT
Selection Sort is one of the most basic sorting algorithms. This algorithm involves
iterating the whole array per element and replacing it with the smallest element
present ahead of it.

The following code represents the Selection Sort implementation in C++ for output
in ascending order :

void SelectionSort(vector<int> a)
{
for(int i = 0; i< a.size() - 1; i++)
{
int mini_index = i;
for(int j = i+1; j< a.size(); j++)
{
if(a[mini_index] > a[j])
{
mini_index = j;
}
}
int temp = a[mini_index];
a[mini_index] = a[i];
a[i] = temp;
}
}
TIME AND SPACE COMPLEXITY OF SELECTION SORT

As we know that Selection Sort iterates over the whole array for each element in
the array , it is evident that it is very inefficient algorithm. But this algorithm
utilizes only one variable irrespective of the size of the array.

For calculating it’s time complexity, We calculate the number of steps it performs
to sort the array.

For every element at ith position , we iterate over (n-i) elements ,

⸫ Time Complexity = 𝑂(∑𝑖<𝑛


𝑖=1 (𝑛 − 𝑖))
𝑛(𝑛−1)
=𝑂( )
2
= 𝑂(𝑛2 ).

⸫ Space Complexity= 𝑂(1)


BEST CASE FOR SELECTION SORT

Best case for selection Sort would be when the input array is in the order we
choose to output , i.e., if we want the output to be in ascending order, the input
array is itself in ascending order.

The below image represents the output in the console when input array was in
ascending order and we wanted the output to be in ascending order.

Here as the no. of useful comparisons is zero, ⸫ the compiler would not have to
spend time in changing mini_index and hence this is the best case.

Space complexity is constant due to constant single variable usage.

Time Complexity = 𝑂(𝑛2 ).

Space Complexity= 𝑂(1)


AVERAGE CASE FOR SELECTION SORT

Average case for the Selection Sort would be when the input array is in jumbled
form , i.e. , neither in ascending or descending order.

The below image represents the output in the console when input array was in
jumbled order and we wanted the output to be in ascending order.

Here as the no. of useful comparisons is average, ⸫ the compiler would have to
spend average time in changing mini_index and hence this is the average case.

Space complexity is constant due to constant single variable usage.

Time Complexity = 𝑂(𝑛2 ).

Space Complexity= 𝑂(1)


WORST CASE FOR SELECTION SORT

Worst case for the Selection Sort would be when the input array is in reverse form
with respect to the required output , i.e. , input is in ascending order and output
required is in descending order.

The below image represents the output in the console when input array was in de
order and we wanted the output to be in ascending order.

Here as the no. of useful comparisons is maximum, ⸫ the compiler would have to
spend maximum time in changing mini_index and hence this is the worst case.

Space complexity is constant due to constant single variable usage.

Time Complexity = 𝑂(𝑛2 ).

Space Complexity= 𝑂(1)


COMPLETE CODE FOR THE RESULTS
#include<bits/stdc++.h>
using namespace std;

void print(vector<int> a)
{
for(int i =0; i< a.size() ; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int s = 0;
vector<int> SelectionSort(vector<int> a)
{
int counter = 0;
for(int i = 0; i< a.size() - 1; i++)
{
int mini_index = i;
for(int j = i+1; j<a.size(); j++)
{
if(a[mini_index] > a[j])
{
mini_index = j;
s++;
}
}
counter++;
int temp = a[mini_index];
a[mini_index] = a[i];
a[i] = temp;
cout<<" Step "<<counter<<"-> ";
print(a);
}
cout<<" No. of useful comparisons -> "<<s<<endl;
return a;
}

int main()
{
vector<int> a = {45, -15 , 389, -23 , 56 , 2};
cout<<" Input array -> ";
print(a);
a = SelectionSort(a);
cout<<endl<<" Final Sorted Array -> ";
print(a);
cout<<endl<<endl<<endl<<" Divyam Agrawal"<<endl<<" CSE"<<endl<<"
20103088"<<endl<<endl;
}

You might also like