Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

PART A

(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.01
A.1 Aim:
Write a program to implement Selection Sort and analyze its complexity.

A.2 Prerequisite:

A.3 Outcome:
After successful completion of this experiment students will be able to analyze
the time complexity of various classic problems.

A.4 Theory:
• Selection sort is a sorting algorithm, specifically an in- HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm"place HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/Comparison_sort" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm"comparison HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/Comparison_sort" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" HYPERLINK
"https://en.wikipedia.org/wiki/In-place_algorithm" sort. It has O(n2) time
complexity, making it inefficient on large lists, and generally performs worse than
the similar insertion sort. Selection sort is noted for its simplicity, and it has
performance advantages over more complicated algorithms in certain situations,
particularly where auxiliary memory is limited.The algorithm divides the input list
into two parts: the sublist of items already sorted, which is built up from left to
right at the front (left) of the list, and the sublist of items remaining to be sorted that
occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted
sublist is the entire input list. The algorithm proceeds by finding the smallest (or
largest, depending on sorting order) element in the unsorted subsist, exchanging
(swapping) it with the leftmost unsorted element (putting it in sorted order), and
moving the sublist boundaries one element to the right.
• Algorithm:
• Start
• for i = 1:n,
• k=i
• for j = i+1:n,
• if a[j] < a[k], k = j
• → invariant: a[k]
• smallest of a[i..n]
• swap a[i,k]
• → invariant: a[1..i] in final position
• end

• Time Complexity:

• The number of iterations of this loop is nnn in the first call, then n−1n-1n−1, then
n−2n-2n−2, and so on. We've seen that this sum, 1+2+⋯+(n−1)+n1 + 2 + \cdots +
(n-1) + n1+2+⋯+(n−1)+n is an arithmetic series, and it evaluates to (n+1)(n/2)
(n+1)(n/2)(n+1)(n/2), or n2/2+n/2n^2/2 + n/2n2/2+n/2. Therefore, the total time for
all calls to indexOfMinimum is some constant times n2/2+n/2n^2/2 + n/2n2/2+n/2.
In terms of big-Θ notation, we don't care about that constant factor, nor do we care
about the factor of 1/2 or the low-order term. The result is that the running time for
all the calls to indexOfMinimum is Θ(n2)\Theta(n^2)Θ(n2).
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no Black
board access available)

Roll No.: C Name:


Class: SE Batch: C
Date of Experiment: Date of Submission:-
Grade:

B.1 Software Code written by student:


(Paste your code completed during the 2 hours of practical in the lab here)

#include<stdio.h>

#include<conio.h>

void main()

int n,i,j,a[100],temp,pos,k;

clrscr();

printf("Enter the number of elements\n");

scanf("%d",&n);

printf("Enter array elements\t");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n-1;i++)

pos=i;
for(j=i+1;j<n;j++)

if(a[pos]>a[j])

pos=j;

if(pos!=i)

temp=a[i];

a[i]=a[pos];

a[pos]=temp;

printf("pass::");

for(k=0;k<n;k++)

printf("\t%d\t",a[k]);

printf("\nThe sorted list is :");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

getch();

B.2 Input and Output:


B.3 Observations and learning:
It has O(n2) time complexity, making it inefficient on large lists, and generally performs
worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has
performance advantages over more complicated algorithms in certain situations,
particularly where auxiliary memory is limited.

For 4 inputs we can find the time complexity as 4*4 = 16.

B.4 Conclusion:
Thus we learned how to implement selection sort in C and find it’s time complexity.

B.5 Question of Curiosity


(To be answered by student based on the practical performed and learning/observations)

Q1: What is difference between algorithm and program?

An algorithm is a sequence of steps which is utilized in order to solve a


computational problem whereas pseudocode is nothing but a more simple form
of an algorithm which involves some part of natural language to enhance the
understandability of the high-level programming constructs or for making it
more human-friendly.

Computer programming contains an enormous part, of generating


unambiguous, sequence-wise procedures for the computer to follow in
order to produce particular results. Algorithms are considered to be the
foundation of computer programming.

Q2: What are different criteria to write algorithm?

• If you want to build algorithms you should learn a lot about existing
algorithms. This accomplishes two things; you will understand what is
already out there so you don’t reinvent the wheel, and if you dig deep
enough you will not only learn what an algorithm does and how it works
but you will learn how people analyze algorithms (like prove that they are
correct and analyze how efficient they are). This is going to require you to
be very comfortable with mathematics and programming; luckily there
are tons of resources on the internet including research papers, moocs,
videos, and blogs.

• If you want to create something new keep in mind that “Necessity is the
mother of invention.” The motivation to solve a specific problem is
usually greater than the motivation to create something arbitrarily.
Having a goal also makes it easier to create because it constrains your
possibilities; when you aren’t solving a real problem it’s easy to come up
with something useless.

Q3: What is time complexity?

In computer science, the time complexity is the computational complexity


that describes the amount of time it takes to run an algorithm. Time
complexity is commonly estimated by counting the number of elementary
operations performed by the algorithm, supposing that each elementary
operation takes a fixed amount of time to perform. Thus, the amount of time
taken and the number of elementary operations performed by the algorithm
are taken to differ by at most a constant factor.

Q4: Derive time complexity if selection sort?

First iteration takes n steps to find the minimum. The second iteration
takes n-1 steps to find the minimum in the non-sorted part ... The last
iteration takes 1 step to find the minimum in the non-sorted part.
After these steps you have an sorted array (even it was sorted before).
Selection sort does not check if the array is already sorted by an linear-
time algorithm. Selection sort repeatedly searches the minimum. That's
the way how selection sort works.

When you repeatedly search the minimum it takes n+(n-1)+...+1 so you


get (n(n+1))/2 = (n²+n)/2 which is in O(n²)

Q5: What is worst case and best case time complexity of selection sort?

Worst-case performance О(n2) comparisons, О(n) swaps


Best-case performance О(n2) comparisons, О(n) swaps

************************

You might also like