0% found this document useful (0 votes)
13 views3 pages

Functions.: S.No: 19 Date: 2022-03-14

Uploaded by

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

Functions.: S.No: 19 Date: 2022-03-14

Uploaded by

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

S.No: 19 Exp.

Name: Write a C program to Sort elements using Insertion Sort Date: 2022-03-14

Aim:

Page No:
Write a program to sort the elements in ascending order with insertion sort technique using
functions.

ID: 214G1A0580
At the time of execution, the program should print the message on the console as:

Enter n value :

For example, if the user gives the input as:

Enter n value : 3

Next, the program should print the message on the console as:

Enter 3 elements :

if the user gives the input as:

Enter 3 elements : 45 67 34

then the program should print the result as:

Elements before sorting : 45 67 34


Elements after sorting : 34 45 67

Note: Do use printf() with ' \n ' at the end of output.


Source Code:

FunctionsWithArrays5.c

#include <stdio.h>
void insertion_sort(int [], int);
void read(int [], int);
void display(int [], int);
void main() {

2021-2025-CSE-B
int a[20], n, i;
printf("Enter n value : ");
scanf("%d", &n);
read(a, n);
printf("Elements before sorting : ");
display(a, n);
insertion_sort(a, n);
Srinivasa Ramanujan Institute of Technology

printf("Elements after sorting : ");


display(a, n);
}
void insertion_sort(int a[],int n) { // Write the arguments
// Write the code
int i,j,k;
for(i=1;i<n;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[j]>k)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=k;
}

Page No:
}
void read(int a[],int n) { // Write the arguments
// Write the code
int i;

ID: 214G1A0580
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[],int n) { // Write the arguments
// Write the code
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter n value : 3
Enter 3 elements : 45 67 34
Elements before sorting : 45 67 34
Elements after sorting : 34 45 67

Test Case - 2

User Output
Enter n value : 5
Enter 5 elements : 2 8 4 1 3

2021-2025-CSE-B
Elements before sorting : 2 8 4 1 3
Elements after sorting : 1 2 3 4 8

Test Case - 3

User Output
Srinivasa Ramanujan Institute of Technology

Enter n value : 6
Enter 6 elements : 23 15 18 12 16 11
Elements before sorting : 23 15 18 12 16 11
Elements after sorting : 11 12 15 16 18 23

Test Case - 4

User Output
Enter n value : 4
Enter 4 elements : -26 -19 -263 -189
Elements before sorting : -26 -19 -263 -189
Elements after sorting : -263 -189 -26 -19
Test Case - 5

User Output

Page No:
Enter n value : 6
Enter 6 elements : -2 -4 2 4 5 1
Elements before sorting : -2 -4 2 4 5 1
Elements after sorting : -4 -2 1 2 4 5

ID: 214G1A0580
2021-2025-CSE-B
Srinivasa Ramanujan Institute of Technology

You might also like