Simple C Programs Using Sorting & Searching : Insertion sort - String C Program

/* Program to sort an element using Insertion sort */
#include <stdio.h>
#include <conio.h>
main()
{
int i,j,k,x,a[100]; /* Local definitions */
printf("Element to be inserted");
scanf("%d", &x);
i=0;
while(x!=‒99)
{
k=i‒1;
while((x<a[k])&&(k> =0))
{
a[k+1]=a[k];
‒ ‒k;
} /* while */
a[k+1]=x;
printf("Array after inserting %d\n",x);
for(j=0;j<=i;j++)
printf("%5d",a[j]);
printf("\n");
printf("\n Element to be inserted (‒99 to stop)?");
scanf("%d", &x);
printf("\n");
++i;
} /* while */
printf("The final sorted array\n");
for(j=0;j<i;j++)
printf("%5d\n",a[j]);
} /* main */
OUTPUT
Element to be inserted 5
Array after inserting 5:
Element to be inserted(‒99 to stop)? 3
Array after inserting 3:
3 5
Element to be inserted(‒99 to stop)? 4
Array after inserting 4:
3 4 5
Element to be inserted(‒99 to stop)? 2
Array after inserting 2:
2 3 4 5
Element to be inserted(‒99 to stop)? 1
Array after inserting 1:
1 2 3 4 5
Element to be inserted(‒99 to terminate)? ‒99
EXPLANATION: Program reads an element into an array, whenever the value entered into an array it compares the corresponding array element and the value will be moved to its appropriate position in an array.
Computer Programming C Laboratory: String C Programs : Tag: C Programming : - C Program to sort an element using Insertion sort
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation