Insertion sort method in Single dimensional array

Insertion sort method is one of the must know the sorting algorithm for each and every computer science student.

In this sorting program, we are sorting an integer array of size 10 using the insertion sort method.  This method is sorting this array into ascending order.

Insertion sorting on a single dimensional array using functions.

/* Insertion sort algorithm
made by :rakesh kumar
*/

#include<iostream>
#include<iomanip>
using namespace std;

void output(int a[],int n){
for(int i=0;i<n;i++)
cout<<setw(10)<<a[i];
}

void input(int x[],int n){
for(int i=0;i<n;i++)
{
cout<<“Enter x[“<<i+1<<“] number : “;
cin>>x[i];
}
}

void insertion_sort(int x[] ,int n)
{
int i,j,low,pos,temp;
for(i=1;i<n;i++) {
temp = x[i];
j= i-1;
while(temp<x[j] && j>=0){
x[j+1]=x[j];
j= j-1;
}
x[j+1]=temp;
}
return;
}

int main()
{
int x[10];
input(x,10);
insertion_sort(x,10);
cout<<“\n sorted Array :”;
output(x,10);
return 0;
}

Other most useful sorting algorithm are

  1. Bubble sort method program using single dimensional array
  2. selection sort method program using single dimensional array

If you need any other program on any sorting method, do post your request in comments.

Download File :

Leave a Reply

Your email address will not be published. Required fields are marked *