Functions.: S.No: 19 Date: 2022-03-14
Functions.: S.No: 19 Date: 2022-03-14
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 :
Enter n value : 3
Next, the program should print the message on the console as:
Enter 3 elements :
Enter 3 elements : 45 67 34
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
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");
}
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