Experiment Title.
: Sorting Of Array
Student Name: Harsh Bhardwaj UID: 19BCS1468
Branch: CSE Section/Group : 3(A)
Semester: 3 Date of Performance:
Subject Name: Data Structure Lab Subject Code: CSP-231
1. Aim/Overview of the practical:
Write a program to sort an array of integers in ascending/descending order using
Merge Sort.
Insertion Sort.
2. Task to be done:
We have to write a code in which we sort a array input by user by Merge sort or Insertion
sort.
3. Algorithm/Flowchart :
Merge Sort :
Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself
for the two halves and then merges the two sorted halves. The merge() function is used for
merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and
arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Insertion Sort :
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
4. Code for experiment/practical:
Merge Sort :
#include<iostream>
using namespace std;
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int n,i;
cout<<"Enter number of elemets of array:"<<endl;
cin>>n;
int a[n];
cout<<"Enter elements of array:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
mergeSort(a, 0, n - 1);
cout<<("Sorted array is:")<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
Insertion Sort :
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements of unsorted array:"<<endl;
cin>>n;
int a[n];
cout<<"Enter elements of unsorted array:"<<endl;
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=1;j<n;j++)
{
for(int k=0;k<j;k++)
{
if(a[j]<a[k])
{
int l=a[j];
a[j]=a[k];
a[k]=l;
}
}
}
cout<<"Sorted array is:"<<endl;
int m;
for(m=0;m<n;m++)
{
cout<<a[m]<<" ";
}
return 0;
}
[Link] /Observation/Complexity :
In terms of moves, merge sort's worst case complexity is O(n log n)—the same complexity as
quicksort's best case, and merge sort's best case takes about half as many iterations as the worst
case. ... Unlike some (efficient) implementations of quicksort, merge sort is a stable sort.
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at
a time. ... Adaptive, i.e., efficient for data sets that are already substantially sorted: the
time complexity is O(kn) when each element in the input is no more than k places away from
its sorted position.
[Link]/Output/Writing Summary:
Merge Sort :
Insertion Sort :
Learning outcomes (What I have learnt):
1. Learn how to sort array using merge sort.
2. Learn how to sort array using insertion sort.
3. Learn time complexity of merge sort.
4. Learn time complexity of insertion sort.
Evaluation Grid:
Sr. No. Parameters Marks Obtained Maximum Marks
1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5