0% found this document useful (0 votes)
23 views7 pages

AOA Experiment 2

The document outlines an experiment to study, implement, and analyze the Insertion Sort algorithm, detailing its theory, best and worst-case analyses, and providing a code implementation. Insertion Sort is described as a simple and efficient sorting method for small or nearly sorted datasets, with a time complexity of O(n²) and O(1) space complexity. The conclusion emphasizes its stability, memory efficiency, and importance in understanding sorting principles.

Uploaded by

ashcraftgamer8
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)
23 views7 pages

AOA Experiment 2

The document outlines an experiment to study, implement, and analyze the Insertion Sort algorithm, detailing its theory, best and worst-case analyses, and providing a code implementation. Insertion Sort is described as a simple and efficient sorting method for small or nearly sorted datasets, with a time complexity of O(n²) and O(1) space complexity. The conclusion emphasizes its stability, memory efficiency, and importance in understanding sorting principles.

Uploaded by

ashcraftgamer8
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/ 7

Experiment No.

2
To implement Insertion Sort
Date of Performance: 22/01/2025

Date of Submission: 29/01/2025


Experiment No. 2
Title: Insertion Sort

Aim: To study, implement and Analyze Insertion Sort Algorithm

Objective: To introduce the methods of designing and analyzing algorithms

Theory:
Insertion sort is a simple sorting algorithm that works similar to the way you sort the
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part.

Example:

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Algorithm and Complexity:

Best case analysis:

• Let size of the input array is n. Total time taken by algorithm is the summation of
time taken by each of its instruction.

• The best case offers the lower bound of the algorithm’s running time.

• When data is already sorted, the best scenario for insertion sort happens.

• In this case, the condition in the while loop will never be satisfied, resulting in
tj = 1.

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Worst case analysis:

• The worst-case running time gives an upper bound of running time for any input.

• The running time of algorithm cannot get worse than its worst-case running time.

• Worst case for insertion sort occurs when data is sorted in reverse order.

• So we must have to compare A[j] with each element of sorted array A[1 … j – 1].
So, tj = j

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Code:

#include <stdio.h>

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


int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
printf("Iteration %d: ", i);
for (int k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n\n");
}
}

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");

insertionSort(arr, n);

printf("Sorted Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");

return 0;
}

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Output:

Conclusion:

Insertion Sort is a simple and intuitive sorting algorithm that efficiently builds a sorted
sequence by inserting each element into its correct position. It performs well for small or
nearly sorted datasets, with an average and worst-case time complexity of O(n²). Unlike more
complex sorting algorithms, it is stable and operates in-place with O(1) extra space, making it
memory efficient. Though not ideal for large datasets, its adaptive nature allows it to perform
efficiently when dealing with partially sorted data. Overall, Insertion Sort is an essential
algorithm for understanding sorting principles and algorithmic design.

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering

You might also like