Insertion Sort in C Program With Explanation

Insertion sort algorithm in c. This program is basically sorting program using the insertion sorting. The values will be initialized after the current values process has been done in insertion sorting in c programming language

#include<stdio.h>
int main()
{
int i, j, count, temp, num[25];
printf("How many nums ");
scanf("%d",&count);
printf("Enter %d elements", count);
for(i=0;i<count;i++)
scanf("%d",&num[i]);
for(i=1;i<count;i++)
{
temp=num[i];
j=i-1;
while((temp<num[j])&&(j>=0))
{
num[j+1]=num[j];
j=j-1;
}
num[j+1]=temp;
}
printf("After sorting");
for(i=0;i<count;i++)
printf(" %d",num[i]);
return 0;
}

 

Output:

How many nums

3

Enter 3 elements

2334 65 1

After sorting 1 65 2334

Temp Variable in C Program

A temporary variable is a variable in computer programming that has a brief lifetime and is used to retain data that will be removed quickly or before it can be stored in a more permanent memory location. It is commonly stated as a local variable, or a variable with local scope, because it is short-lived. A temp variable is a temporary variable that can be used in a C program to swap two numbers or temporarily assign any value.

Return Statement in C

A return statement instructs the program to exit the current function and resume execution at the point in the code immediately following the function’s call. Return statements allow a function to provide a return value that will be given back to the code that invoked the function in many languages.

While Loop to Execute Insertion Sort Algorithm in C

In C programming, a while loop runs a target statement repeatedly as long as a condition is true. A while loop is a control flow statement in most computer programming languages that allows code to be performed repeatedly based on a supplied Boolean condition. The while loop is similar to a looping if statement.