Algo Assignment-04 Insertion Sort

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ALGORITHM PRACTICAL ASSIGNMENTS (SOLUTIONS)

GROUP-B
(Sorting: Quadratic & logarithmic time complexities)

Assign- 1) Implement Insertion Sort (The program should report the number of
pass & comparisons)

PROCEDURE

Insertion sort is a simple sorting algorithm that works by building a sorted array
one element at a time. It is considered an “in-place” sorting algorithm, meaning it
doesn’t require any additional memory space beyond the original array.
To achieve insertion sort, follow these steps:
• We have to start with second element of the array as first element in the
array is assumed to be sorted.
• Compare second element with the first element and check if the second
element is smaller then swap them.
• Move to the third element and compare it with the second element, then
the first element and swap as necessary to put it in the correct position
among the first three elements.
• Continue this process, comparing each element with the ones before it and
swapping as needed to place it in the correct position among the sorted
elements.
• Repeat until the entire array is sorted.

Working of Insertion Sort Algorithm:


Consider an array having elements: {23, 1, 10, 5, 2}

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 1


ALGORITHM / PSEUDOCODE

Algorithm
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

Pseudocode
Insertion-Sort(A)
for j = 2 to A.length
key = A[j]
i=j–1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 2


PROGRAM

/* Insertion Sort */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void InsertionSort(int arr[], int n)


{
FILE *out;
out=fopen("Algo\\Output.txt", "a+");
int i, key, j, pass=0, com=0, tcom=0;
for (i = 1; i < n; i++)
{
com=0;
key = arr[i];
j = i - 1;

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


{
arr[j + 1] = arr[j];
j = j - 1;
com++;
}
arr[j + 1] = key;
tcom+=com;
pass++;
fprintf(out, "Pass:%d Comparison:%d\n", pass, com);
}
fprintf(out, "\nTotal Pass:%d Total Comparison:%d", pass, com);
fprintf(out, "\nSorted List:\n");
for(i=0;i<n;i++)
fprintf(out, "%d ", arr[i]);
fclose(out);
printf("Done");
}

void main()
{
clrscr();
FILE *in;
int n, i, Item, a[100];
cout<<"Enter Sample Size (No of Elements):";
cin>>n;

in=fopen("Algo\\Input.txt", "r");
i=0;

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 3


while(!feof(in))
{
fscanf(in, "%d", &a[i++]);
}
fclose(in);

InsertionSort(a, n);
getch();
}

OUTPUT

Do yourself

DISCUSSION

We take an unsorted array for our example.

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is
in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 4


And finds that 33 is not in the correct position. It swaps 33 with 27. It also
checks with all the elements of sorted sub-list. Here we see that the sorted
sub-list has only one element 14, and 27 is greater than 14. Hence, the
sorted sub-list remains sorted after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with


10. These values are not in a sorted order.

So they are swapped.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 5


Again we find 14 and 10 in an unsorted order.

We swap them again.

By the end of third iteration, we have a sorted sub-list of 4 items.

This process goes on until all the unsorted values are covered in a sorted
sub-list.

TIME & SPACE COMPLEXITY

Time Complexity
• Best case: O(n), If the list is already sorted, where n is the number of
elements in the list.
• Average case: O(n2), If the list is randomly ordered
• Worst case: O(n2), If the list is in reverse order

Space Complexity
• Auxiliary Space: O(1)

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 6


Literature Review

Advantages of Insertion Sort:


• Simple and easy to implement.
• Stable sorting algorithm.
• Efficient for small lists and nearly sorted lists.
• Space-efficient.
Disadvantages of Insertion Sort:
• Inefficient for large lists.
• Not as efficient as other sorting algorithms (e.g., merge sort, quick sort) for
most cases.
Applications of Insertion Sort:
Insertion sort is commonly used in situations where:
• The list is small or nearly sorted.
• Simplicity and stability are important.

S. P. Dua (Computer Science, Midnapore College, 7872361926, www.gvtcs.com) 7

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