Insertion and Merge Sort 19BCS2244 AVNISH
Insertion and Merge Sort 19BCS2244 AVNISH
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:
Algorithm:
1.For Insertion 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:
#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;
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];
return 0;
}
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;
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
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.