0% found this document useful (0 votes)
35 views12 pages

8612lab Manual EXPT No. 1 AOA - Selection Sort

Uploaded by

danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

8612lab Manual EXPT No. 1 AOA - Selection Sort

Uploaded by

danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PART A

(PART A : TO BE REFFERED BY STUDENTS)

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:

Selection sort is a sorting algorithm, specifically an in-placecomparison 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)

Roll No.: A05 Name: PRASHANTH NAIDU


Class: SE-A Batch: A1
Date of Experiment: 08/02/24 Date of Submission 08/02/24
Grade:

B.1 Software Code written by student:

*Selection Sort *
#include<stdio.h>

#include<conio.h>

int main()

int i,j,n,t,a[50];

printf("Enter the size of the array:\t");

scanf("%d",&n);

if(n<=50)

printf("Enter the elements of the array:\n");

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

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

printf("The Sorted Array in Selection Sorting Method:\n");

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

printf("Can't create the array");

exit(0);

return 0; }

*Insertion Sort*
#include <stdio.h>

void insertionSort(int arr[], int n) {

int i, key, j;
for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

void printArray(int arr[], int n) {

int i;

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");


printArray(arr, n);

insertionSort(arr, n);

printf("\nSorted array: \n");

printArray(arr, n);

return 0;

B.2 Input and Output:

SELECTION SORT
Insertion
Sort

B.3 Conclusion:

In conclusion, the implemented Selection Sort and Insertion


Sort algorithms showcase contrasting behaviors. While both have
a worst-case time complexity of O(n^2), Insertion Sort exhibits
superior performance with a best-case time complexity of O(n)
when the array is already sorted. This experiment underscores
the importance of selecting the appropriate sorting algorithm
based on the characteristics of the input data to optimize
efficiency.
B.4 Question of Curiosity
Q1: What is difference between algorithm and program?
Q2: What are different criteria to write algorithm?

Q3: What is time complexity?


Q4: Derive time complexity if selection sort & Insertion Sort?
Q5: What is worst case and best case time complexity of selection sort &
Insertion Sort?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy