0% found this document useful (0 votes)
34 views9 pages

Insertion and Merge Sort 19BCS2244 AVNISH

The document describes an experiment to apply insertion and merge sort algorithms to sort an array of numbers and analyze their time and space complexities, with insertion sort having a time complexity of O(n^2) and space complexity of O(1), while merge sort has a time and space complexity of O(nlogn).

Uploaded by

Ashutosh Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Insertion and Merge Sort 19BCS2244 AVNISH

The document describes an experiment to apply insertion and merge sort algorithms to sort an array of numbers and analyze their time and space complexities, with insertion sort having a time complexity of O(n^2) and space complexity of O(1), while merge sort has a time and space complexity of O(nlogn).

Uploaded by

Ashutosh Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Student Name: UID:

Branch:CSE Section:16-A
Semester:3rd Date of Perfomance:28-09-2020
Subject Name:Data Structures Lab Subject Code:CSP-231

Aim:
1. Illustrate Insertion Sort and Merge Sort on the following array: 44, 33, 11, 55, 77, 90, 33,
55, 99,22, 88, 66
2. What is the time and space complexity for both?

Objective:

1.Our objective is to learn about different sorting algorithms.


2.We are going to learn about merge and insertion sort in the
array.
3.Time and space complexities of these algorithms.

Algorithm:
1.For Insertion Sort:

2.For Merge Sort:

1. Divide the array into 2 equal parts.Then there is we have to halves of array.
2. Then again we are going to divide the array recursively .

3.Then we merge these divided arrays into one to get fully sorted array.

Code:

Code for Insertion Sort:

#include<stdio.h>

int main()

int array[12]={44,33,11,55,77,90,33,55,99,22,88,66};

int ke;

int i,j;

int temporary;

for(j=1;j<12;j++)

ke=array[j];
i=j-1;

while((i>=0)&&(array[i]>=ke))

temporary=array[i+1];

array[i+1]=array[i];

array[i]=temporary;

i=i-1;

array[i+1]=ke;

printf("elements after sorting using insertion sort are \n");

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

printf("%d \n",array[i]);

return 0;
}
Code for Merge Sort:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 12

void merge(int a[], int tmp[], int left, int mid, int
right);
void msort(int a[], int tmp[], int left, int right);
void merge_sort(int a[], int tmp[], const int size);
void display(int a[],const int size);

int main()
{
int
a[SIZE]={44,33,11,55,77,90,33,55,99,22,88,66};
int tmp[SIZE];

printf("--- C Merge Sort Demonstration --- \n");

printf("Array before sorting:\n");


display(a,SIZE);

merge_sort(a, tmp, SIZE);


printf("Array after sorting:\n");
display(a,SIZE);

return 0;
}

void merge_sort(int a[], int tmp[], const int size)


{
msort(a, tmp, 0, size - 1);
}

void msort(int a[], int tmp[], int left, int right)


{
int mid;
if (right > left)
{
mid = (right + left) / 2;
msort(a, tmp, left, mid);
msort(a, tmp, mid + 1, right);
merge(a, tmp, left, mid + 1, right);
}
}

void merge(int a[], int tmp[], int left, int mid, int
right)
{
int i, left_end, count, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
count = right - left + 1;

while ((left <= left_end) && (mid <= right))


{
if (a[left] <= a[mid])
{
tmp[tmp_pos] = a[left];
tmp_pos = tmp_pos + 1;
left = left +1;
}
else
{
tmp[tmp_pos] = a[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}

while (left <= left_end)


{
tmp[tmp_pos] = a[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
tmp[tmp_pos] = a[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}

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


{
a[right] = tmp[right];
right = right - 1;
}
}

void display(int a[],const int size)


{
int i;
for(i = 0; i < size; i++)
printf("%d ",a[i]);

printf("\n");
}
Discussions:

We have learnt about the different techniques to sort the array.In this experiment we
apply insertion and merge sort.We learnt about the time and space complexity of
these algo’s.
Time and space complexity of insertion sort:

Worst complexity: n^2

Average complexity: n^2

Best complexity: n

Space complexity: 1

Time and space complexity of merge sort:

Worst complexity: n*log(n)

Average complexity: n*log(n)

Best complexity: n*log(n)

Space complexity: n

Learn Outcomes:
1.We learn about the different sorting techniques.
2.Now we know about merge and insertion sort
3.We also learnt about time and space complexity of these sorting techniques.

You might also like