8612lab Manual EXPT No. 1 AOA - Selection Sort
8612lab Manual EXPT No. 1 AOA - Selection Sort
Experiment No.01
A.1 Aim:
Write a program to implement Selection Sort & Insertion 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:
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)
*Selection Sort *
#include<stdio.h>
#include<conio.h>
int main()
int i,j,n,t,a[50];
scanf("%d",&n);
if(n<=50)
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
else
exit(0);
return 0; }
*Insertion Sort*
#include <stdio.h>
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i;
printf("\n");
int main() {
insertionSort(arr, n);
printArray(arr, n);
return 0;
SELECTION SORT
Insertion
Sort
B.3 Conclusion: