Professional Documents
Culture Documents
Experiment No.01: Part A
Experiment No.01: Part A
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)
#include<stdio.h>
#include<conio.h>
void main()
int n,i,j,a[100],temp,pos,k;
clrscr();
scanf("%d",&n);
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]);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
B.4 Conclusion:
Thus we learned how to implement selection sort in C and find it’s time complexity.
• 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.
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.
Q5: What is worst case and best case time complexity of selection sort?
************************