## INDEX

### 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.

1. 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

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

2. #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;
}

3. tu muy bien

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

5. amazing program . GREAT WORK GUYS !!!!

6. RAMBABU TATYASAHEB INGLE12/11/12, 11:37 AM

program changla ahe . akdum mast

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

1. 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

8. good work!!!!!

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

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

10. 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 :)

1. cool..! and correct

11. I thnk its bst way to impliment dir sorting.

12. #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????

1. bubble sort

13. bubble sort i guess

14. This comment has been removed by the author.

15. #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;

}

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

17. the great code of sorting !