C PROGRAM FOR INSERTION SORT





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

#include<stdio.h>
int main(){

  int i,j,s,temp,a[20];

  printf("Enter total elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);

  for(i=1;i<s;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("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting:  0 2 3 7 9





5. Write a c program for heap sort.
7. Write a c program for shell sort.

24 comments:

sandeep said...

in your insertion sort swapping of value is not possible
because
you assign value of a[j] to a[j+1]
but not alloting 0 anywhere in array

Anonymous said...

#include

int main()
{
int a[3],i,j,temp;
printf("Enter 5 Array Element:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
temp=a[i+1];
j=i;
while(temp=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("Sorted Array id: ");
for(i=0;i<3;i++)
{
printf("%d",a[i]);
}
return 0;
}

Anonymous said...

tu muy bien

Anonymous said...

hey this is a great program for insertion sort !!!!!!

RUSHI.C said...

amazing program . GREAT WORK GUYS !!!!

RAMBABU TATYASAHEB INGLE said...

program changla ahe . akdum mast

Anonymous said...

I think The for loop is being started with i=1 instead of i=0. .dats y the program is ok!

Anonymous said...

good work!!!!!

Unknown said...

actually the program has to be started for i=1 ...because in insertion sort we always compare with the element before it.in case of the 0th element there is nothing to compare with as there is nothing before it. so thats why the loop is ok

Anonymous said...

u have taken an arry of 3 but u want to take 5 element. how is it possible????

Anonymous said...

Well, if you want to use for loop instead of while, it goes something like this-

for(i=1;i0;j--)
{ if(a[j]<a[j-1])
{ temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
The rest is the same :)

Anonymous said...

0 is allotting in j=i-1 which then proceed.....

Anonymous said...

ur array will take only 3 elements and the remaining 2 elements wwill get stored in successive locations in memory following ur array!

Anonymous said...

I thnk its bst way to impliment dir sorting.

Unknown said...

#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=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????

Anonymous said...

cool..! and correct

Unknown said...

bubble sort i guess

Unknown said...
This comment has been removed by the author.
Unknown said...

#include
#include
int main()
{
int n, array[1000], i, j, temp;

printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);

for (i = 0 ; i < n; i++) {
scanf("%d", &array[i]);
}
for (i = 0 ; i < n-1; i++) {

for( j = i+1 ; j > 0; j--){

if(array[j] < array[j-1]) /* if(array[j] > array[j-1])*/
{
temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++) {
printf("%d\n", array[i]);
}
getch();
return 0;

}

Unknown said...

bubble sort

Unknown said...

in while loop there must be one more statement
after
a[j+1]=a[j];
a[j]=temp;

Unknown said...

the great code of sorting !

Unknown said...

Very informative article.Thank you author for posting this kind of article .

http://www.wikitechy.com/view-article/insertion-sorting-

program-in-cpp-with-example-and-explanation



Both are really good,
Cheers,
Venkat

Unknown said...

what kind of techinque was used?