You are on page 1of 8

EXPERIMENT NUMBER- 04

STUDENT’S NAME RAJ BANSAL


STUDENT’S UID 20CBS1038
CLASS AND GROUP 20CSBS / A
SEMESTER 2nd SEMESTER

1.TOPIC OF EXPERIMENT: INSERTION SORTING AND MERGE SORTING.

2.AIM OF THE EXPERIMENT:


PRACTICAL 4.1 →

Program to arrange the elements of an array in ascending order using insertion sort method.

PRACTICAL 4.2 →

Program to arrange the elements of an array in ascending order using merge sort method.

3.FLOWCHART/ALGORITHM:
Practical 4.1

1. Repeat steps 2 - 4 to k=2,3,4,5,……….,N


2. Set temp=a[k] & J=k-1
3. Repeat while Temp a[j] & j>=1
[A.] Set a[j+1]=a[j]
[B.] Set j=j+1

4. Set a[j+1]= temp

5. Exit.

Practical 4.2

1. Set i=1,j=1,k=1

2. Repeat Steps till i<=m&&j<=n

If(a[i]<b[j])

[a] Set c[k]=a[I]

[b] Update k=k+1 & I=I+1;

Else

[a] Set c[k]=a[I]

[b] Update k=k+1 & I=I+1;

Subject Name: Data Structure Lab Subject Code:CSP-173


3. if(I>m)

Repeat steps till(j<=n)

C[k]=b[j]

J=j+1; k=k+1;

4. if(j>n)
Repeat steps till(I<=m)

[a] c[k]=a[I]

[b] Update c=I+1;k=k+1;

5. Exit

4.PROGRAM CODE:
Practical 4.1

#include<stdio.h>

int main()

int a[5],k,j,i,temp;

printf("Enter 5 array elements:");

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

scanf("%d",&a[i]);

for(k=1;k<5;k++)

temp=a[k];

j=k-1;

while((a[j]<temp)&&(j>=0))

a[j+1]=a[j];

j=j-1;

a[j+1]=temp;

Subject Name: Data Structure Lab Subject Code:CSP-173


printf("Sorted array using insertion sort:");

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

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

Practical 4.2

#include <stdio.h>

int a[10];

int b[10];

void merging(int low, int mid, int high)

int l1, l2, i;

l1 = low;

l2 = mid + 1;

i = low;

while( l1 <= mid && l2 <= high)

if(a[l1] <= a[l2])

b[i++] = a[l1++];

else

b[i++] = a[l2++];

Subject Name: Data Structure Lab Subject Code:CSP-173


while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];

for(i = low; i <= high; i++)

a[i] = b[i];

void msort(int low, int high)

int mid;

if(low < high)

mid = (low + high) / 2;

msort(low,mid);

msort(mid+1,high);

merging(low,mid,high);

int main()

Subject Name: Data Structure Lab Subject Code:CSP-173


{
int i;
printf("Enter elements\n");
for(i = 0; i <=9; i++)
scanf("%d",&a[i]);
msort(0,9);
printf("\nList after sorting\n");
for(i = 0; i <=9; i++)
printf("%d ", a[i]);
}

5.ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION:


(Kindly jot down the compile time errors encountered)
Syntax Error
; missing
} missing
Input error
Segmentation Fault

Subject Name: Data Structure Lab Subject Code:CSP-173


6.PROGRAMS’ EXPLANATION (in brief):

Practical 4.1

The time complexity of the given Insertion Sort program is O(n^2).

The space complexity of the given Insertion Sort program is O(1).

Worst complexity: O(n^2)

Average complexity: O(n^2)

Best complexity: O(n)

Practical 4.2

The time complexity of the given Selection Sort program is O(n log n).

The space complexity of the given Selection sort program is O(1).

Worst complexity: O(n log n)

Average complexity: O(n log n)

Best complexity: O(n log n)

Subject Name: Data Structure Lab Subject Code:CSP-173


7.OUTPUT:

Practical 4.1

Practical 4.2

Subject Name: Data Structure Lab Subject Code:CSP-173


8.LEARNING OUTCOMES:

• Analyze and compare the efficiency and properties of various data structures.

• Identify strength and weaknesses of various data structures.

• Design and employ appropriate data structures for solving computing problems.

• Possess the ability to design efficient algorithms for solving computing problems.

EVALUATION COLUMN (To be filled by concerned faculty only)

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Prelab questions 5

2. Completion of worksheet with learning 10


outcomes and program’s output along with
cleanliness and discipline.
3. Post lab Questions 5

4. Total Marks 20

5. Teacher’s Signature (with date)

Subject Name: Data Structure Lab Subject Code:CSP-173

You might also like