0% found this document useful (0 votes)
3 views8 pages

Exp 1

The document outlines an experiment to implement Insertion Sort and Selection Sort algorithms, detailing their respective algorithms and complexities. Insertion Sort is efficient for nearly sorted data with a best-case time complexity of O(n), while Selection Sort consistently performs at O(n²) regardless of input order. The conclusion emphasizes the strengths and weaknesses of both algorithms, suggesting that Insertion Sort is better for stability and adaptability, while Selection Sort is preferable when minimizing swaps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Exp 1

The document outlines an experiment to implement Insertion Sort and Selection Sort algorithms, detailing their respective algorithms and complexities. Insertion Sort is efficient for nearly sorted data with a best-case time complexity of O(n), while Selection Sort consistently performs at O(n²) regardless of input order. The conclusion emphasizes the strengths and weaknesses of both algorithms, suggesting that Insertion Sort is better for stability and adaptability, while Selection Sort is preferable when minimizing swaps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No.

01
Name: Vedaant Dinesh Ambolkar Roll No: 05
Date:28/1/2025

Aim: To implement Insertion Sort and Selection Sort.

Theory:
Insertion sort
Selection sort

Algorithms:
Algorithm INSERTION-SORT()
for i = 1 to n-1:
key = array[i]
j=i-1

while j >= 0 and array[j] > key:


array[j + 1] = array[j]
j=j-1

array[j + 1] = key

Algorithm SELECTION-SORT()

for i = 0 to n-2:

min_index = i

for j = i+1 to n-1:

if array[j] < array[min_index]:

min_index = j
if min_index != i:

swap array[i] with array[min_index]

Complexity:
Insertion Sort:
Best Case Analysis: O(n)
Worst Case Analysis: O(n^2)
Average Case Analysis: O(n^2)

Selection Sort:
Best Case Analysis: O(n^2)
Worst Case Analysis: O(n^2)
Average Case Analysis: O(n^2)

Program 1: Insertion Sort and output

#include<stdio.h>

#include<conio.h>

void main()

{ int key;

int n=10,i,j;

int arr[10];

printf("enter the number of elements in the array :");

scanf("%d",&n);
for(i=0;i<n;i++)

printf("enter the valus %d :",i+1);

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

printf("the array you entered is :");

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

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

printf("\n");

for(j=1;j<=n-1;j++)

key =arr[j];

i=j-1;
while(i>=0 && arr[i]>key)

arr[i+1]=arr[i];

i=i-1;

arr[i+1]=key;

printf("the sorted arry is :");

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

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

getch();

OUT PUT
Program 2: Selection Sort and output

#include<stdio.h>

#include<conio.h>

void sorting(int arr[],int n)

int i,j,temp;

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

int min=i;

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

if(arr[j] < arr[min]){

min = j;
}

temp = arr[min];

arr[min] = arr[i];

arr[i] = temp;

int main(){

int i;

int arr[] ={64,25,22,12,11};

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

clrscr();

sorting(arr , n);

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

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

getch();

return 0;
}

OUT PUT

Conclusion: write a comparative analysis of both algorithms as your conclusion in


your own words.

ANS=> nsertion Sort and Selection Sort are both simple, comparison-based
sorting algorithms, but they differ in their approaches and efficiency.

Insertion Sort works similarly to how we arrange playing cards in our hands.
It builds the sorted sequence one element at a time by taking each element
and inserting it into its correct position within the already sorted portion of
the array. This makes it efficient when dealing with nearly sorted data, as its
best-case time complexity is O(n). However, in the worst and average cases,
its time complexity is O(n²), making it less efficient for large datasets.
On the other hand, Selection Sort works by repeatedly finding the smallest
element from the unsorted part and swapping it with the first unsorted
element. Unlike Insertion Sort, it does not adapt well to nearly sorted data
since it always performs O(n²) comparisons regardless of the input order.
However, it makes at most O(n) swaps, making it more efficient in scenarios
where swapping is costly.

In summary, if stability and adaptability to nearly sorted data are important,


Insertion Sort is the better choice. If minimizing swaps is a priority, Selection
Sort is preferable. However, for large datasets, both algorithms are
inefficient compared to more advanced sorting techniques like Merge Sort or
Quick Sort.

You might also like