Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting.

**Logic :** Here, sorting takes place by inserting a particular element at the appropriate position, that’s why the name- insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.

If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and bubble sorting, which follows in the next pages.

**C program for Insertion Sort:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include void main() { int A[20], N, Temp, i, j; clrscr(); printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp<A[j] && j>=0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } |

**Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.**

i need a unique & short program using insertion sort ?? can you help me ?

Source code of simple merge sort implementation using array in ascending order in c programming language

#include

#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);

void partition(int arr[],int low,int high);

int main(){

int merge[MAX],i,n;

printf(“Enter the total number of elements: “);

scanf(“%d”,&n);

printf(“Enter the elements which to be sort: “);

for(i=0;i<n;i++){

scanf("%d",&merge[i]);

}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");

for(i=0;i<n;i++){

printf("%d ",merge[i]);

}

return 0;

}

void partition(int arr[],int low,int high){

int mid;

if(low<high){

mid=(low+high)/2;

partition(arr,low,mid);

partition(arr,mid+1,high);

mergeSort(arr,low,mid,high);

}

}

void mergeSort(int arr[],int low,int mid,int high){

int i,m,k,l,temp[MAX];

l=low;

i=low;

m=mid+1;

while((l<=mid)&&(m<=high)){

if(arr[l]mid){

for(k=m;k<=high;k++){

temp[i]=arr[k];

i++;

}

}

else{

for(k=l;k<=mid;k++){

temp[i]=arr[k];

i++;

}

}

for(k=low;k<=high;k++){

arr[k]=temp[k];

}

}

Sample output:

Enter the total number of elements: 5

Enter the elements which to be sort: 2 5 0 9 1

After merge sorting elements are: 0 1 2 5 9

hii..i want to know the meaning of why we use j=j-1 after assigning temp to a[i] in insertion sort..please explain it as soon as possible my mail id is [email protected].

main()

#define size 10

{

int arr[size],i,j,item;

printf(“\n enter the element in array”);

for(i=0;i<size;i++)

scanf("%d",&arr[i]);

for(j=0;j<size;j++)

{ item=arr[i];

for(i=j-1;item=0;i–)

{ arr[i+1]=arr[i];

arr[i]=item;

}}

thnxx.

i understood insertion stood.

For balchhal programs contact AOT guys….West bengal…chodao bara

Rohon dar gude batha…tai boli dudu khao..

sir,i want to know what is use of gotoxy()?

multy way tree

#include

#include

void main()

{

int a[20],k,n,i=0,j=0,t=0;

clrscr();

printf(“no of of elemts”);

scanf(“%d”,&n);

printf(“enter %d values”,n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=1;i=1;j–)

{

printf(“i=%d j=%d %d %d”,i,j,a[j],a[j-1]);

if(a[j]>a[j-1])

break;

else

{

t=a[j];

a[j]=a[j-1];

a[j-1]=t;

}

} printf(“\n”);

} printf(“\n”);

for(i=0;i<n;i++)

printf(" %d ",a[i]);

getch();

}

THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????

DOES IT REPRESENT INSERTION SORT????

m confused in insertion sort …..when comparison is done and 1st pass completes then how elements compare with all the previous elements there is no loop ……pls explain…..

Well explained. I assume there is one error in the code during runtime. Changed below code.

while( j>=0 && Temp<A[j] )

{

A[j+1] = A[j];

j = j-1;

}

thanks

#include

using namespace std;

int main()

{ int k,A[6];

for(int i=0;i>A[i];

}

for(int i=1;i=0 && A[j]>A[i])

{

A[j+1]=A[j];

j–;

}

A[j+1]=value;

}

cout<<"sorted list"<<endl;

for( k=0;k<6;k++)

{

cout<<A[k]<<"\t";

}

return 0;

}

/* wht give the wrong output?? i tryed to check it …plz help*/

Hey! Remarkable blog post! I favor the way you explained

Insertion sort in C program . One another terrific review from the

amazing publisher. We appreciate your the individual blog!

It is usually wonderful to enjoy a book everyone works.

If only Designed certain superior ability as a copywriter.

I am a learner and i also are required to come up with hundreds of newspaper

publishers relentlessly .

Defending that there exists exists world wide web sites such

as this one writing services review which planning reviewed high on different essay having

providers