DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
EXPERIMENT – 2.2
Student Name: Aman Kumar UID: 21BCS5218
Branch: CSE Section/Group: 617/B
Semester: 3rd Date of Performance: 14/09/22
Subject Name: Data Structure Subject Code: 21CSH-211
1. Aim of the practical:
To sort an array using insertion sort technique
2. Objective
Write a program to sort an array of integers in ascending/descending order using -
Insertion Sort.
3. Introduction
Insertion sort is a simple sorting algorithm that works similar to the way you sort
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.
4. Software Used
Online C Compiler
5. Hardware requirements:
Processor= AMD RYZEN 3
RAM= 8.00 GB
SSD= 512 GB
Keyboard and Mouse.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Algorithm:
Step 1 - If the element is the first element, assume that it is already sorted.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then
move to the next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
7. Program code:
#include <iostream>
using namespace std;
void insertion_asc(int a[], int n){ int i, j, temp;
for(i=1; i<n; i++){//first is already sorted temp = a[i];
j = i-1;
while(j>=0 && a[j]>temp){
a[j+1] = a[j];
j = j-1;//compare with previous elements
}
a[j+1] = temp;
cout<<"\nAfter "<<i<<" iteration: \n"; for(int k=0; k<n; k++){
cout<<a[k]<<" ";
}
cout<<endl;
}
}
void insertion_dsc(int a[], int n){ int i, j, temp;
for(i=1; i<n; i++){//first is already sorted temp = a[i];
j = i-1;
while(j>=0 && a[j]<temp){ a[j+1] = a[j];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
j = j-1;//compare with previous elements
}
a[j+1] = temp;
cout<<"\nAfter "<<i<<" iteration: \n"; for(int k=0; k<n; k++){
cout<<a[k]<<" ";
}cout<<endl;
}
}
int main(){ int choice;
{
printf("Aman Kumar 21BCS5218\n ");;
}
cout<<"1) Sort in Ascending Order\n2) Sort in Descending Order\n"; cout<<"Enter your choice: ";
cin>>choice; int n;
cout<<"Enter the size of the array: "; cin>>n;
int arr[n];
cout<<"Enter the elements of the array: \n"; for(int i=0; i<n; i++){
cout<<"Element "<<i+1<<": ";
cin>>arr[i];
}
cout<<"\nOriginal Array:\n"; for(int i=0; i<n; i++){ cout<<arr[i]<<" ";
}cout<<endl;
switch(choice){
case 1: insertion_asc(arr, n);
cout<<"\nFinal Sorted Array:\n"; for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl; break;
case 2:insertion_dsc(arr, n);
cout<<"\nFinal Sorted Array:\n"; for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
8. Output:
Descending Order
Ascending Order
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
9. Additional Creative Inputs (If Any):
Took input from the user.
10. Learning Outcomes
1) Meaning of sorting algorithms.
2) Understood the working and execution of Insertion sort.
3) Implemented the Insertion sort code to arrange integers in ascending and
descending order.
4) Learnt the importance of sorting algorithms.
5) Learnt the time complexity of Insertion sort.
Evaluation Grid :
Sr. No. Parameters Marks Obtained Maximum Marks
1. Student Performance 12
(Conduct of
experiment)
objectives/Outcomes.
2. Viva Voce 10
3. Submission of 8
Work Sheet
(Record)
Total 30