0% found this document useful (0 votes)
10 views3 pages

DSA Lab Manual(Insertion Sort )

The document provides a comprehensive overview of the Insertion Sort algorithm, including its definition, characteristics, and implementation in C++. It outlines the algorithm's time and space complexities, along with a step-by-step example and C++ code. Additionally, it includes lab exercises and viva questions to reinforce understanding of the algorithm.

Uploaded by

Irfan Ul Haq
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)
10 views3 pages

DSA Lab Manual(Insertion Sort )

The document provides a comprehensive overview of the Insertion Sort algorithm, including its definition, characteristics, and implementation in C++. It outlines the algorithm's time and space complexities, along with a step-by-step example and C++ code. Additionally, it includes lab exercises and viva questions to reinforce understanding of the algorithm.

Uploaded by

Irfan Ul Haq
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/ 3

Data Structures and Algorithms Lab Manual

Topic: Insertion Sort


Objective:

To understand and implement the Insertion Sort algorithm in C++.

Theory:

What is Insertion Sort?

Insertion Sort is a simple and efficient sorting algorithm that works similarly to the way you
might sort playing cards in your hands. It builds the sorted list one item at a time by repeatedly
picking the next item and inserting it into its correct position.

Key Characteristics:

• Time Complexity:
o Best Case: O(n) (when the array is already sorted)
o Worst Case: O(n²) (when the array is sorted in reverse order)
o Average Case: O(n²)
• Space Complexity: O(1) (In-place sorting algorithm)
• Stability: Insertion Sort is a stable sorting algorithm.
• Adaptive: Performs well when the array is already "nearly" sorted.

Algorithm:

1. Start with the second element (index 1) as the key.


2. Compare the key with the elements before it.
3. Shift all elements larger than the key one position to the right.
4. Insert the key in its correct position.
5. Repeat for all elements in the array.

Example Walkthrough:

Input:

Array: [5, 3, 8, 6, 2]

Iterations:
1. Key = 3; Compare with 5, shift 5; Insert 3. Result: [3, 5, 8, 6, 2]
2. Key = 8; Compare with 5; No shifts; Insert 8. Result: [3, 5, 8, 6, 2]
3. Key = 6; Compare with 8 and 5; Shift 8; Insert 6. Result: [3, 5, 6, 8, 2]
4. Key = 2; Compare with all elements; Shift all; Insert 2. Result: [2, 3, 5, 6, 8]

C++ Code Implementation:


#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original Array: ";
printArray(arr, n);
insertionSort(arr, n);
cout << "Sorted Array: ";
printArray(arr, n);
return 0;
}

Lab Exercise:

1. Write the code for Insertion Sort as given above.


2. Test the algorithm with the following inputs:
o Input 1: [10, 7, 8, 9, 1, 5]
o Input 2: [1, 2, 3, 4, 5]
o Input 3: [5, 4, 3, 2, 1]
3. Modify the program to sort an array of floating-point numbers.
4. Analyze the time complexity for each input.
Viva Questions:

1. What is the best-case time complexity of Insertion Sort?


2. Why is Insertion Sort considered an adaptive algorithm?
3. Can Insertion Sort be used for linked lists? Explain why or why not.
4. Compare Insertion Sort with Selection Sort.
5. What is the significance of In-place sorting?

Conclusion:

Insertion Sort is a fundamental sorting algorithm suitable for small or nearly sorted datasets. It
provides a foundation for understanding more complex sorting algorithms.

You might also like