Program to Implement Insertion Sort in CPP (C plus plus)

Program to Implement Insertion Sort in CPP (C plus plus)

In this tutorial, we will learn about the Program to Implement Insertion Sort in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{	
	int array[30];
	int tmp;
    int i;
	int j;
	int k;
	
	
    cout<<"Please enter the number of values:";
    cin>>k;
    
    cout<<endl<<"Please enter the desired values: "<<endl;
 
    for(i=0;i<k;i++)
    {
        cin>>array[i];
    }
 
    for(i=1;i<=k-1;i++)
    {
        tmp=array[i];
        j=i-1;
 
        while((tmp<array[j])&&(j>=0))
        {
            array[j+1]=array[j];    
            j=j-1;
        }
 
        array[j+1]=tmp; 
    }
 
    cout<<endl<<"Sorted list is shown below: "<<endl;
    
    for(i=0;i<k;i++)
    {
        cout<<array[i]<<" ";
    }
 
    return 0;
}

Output:

Program to Implement Insertion Sort in CPP (C plus plus)
Program to Implement Insertion Sort in CPP (C plus plus)
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.