Wednesday, August 17, 2011

QUES 16 Insertion Sort


C LANGUAGE BOOK











WAP TO SORT THE NUMBER USING INSERTION SORT ENTER BY THE USER.







#include<stdio.h>

#define SIZE 100

void Inser_Sort(int q, int a[]);

int main()
{
int i,q;
int a[SIZE];

/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered :");
  scanf("%d", &q);

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a[i]);
}

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a[i]);
}
clrscr();
/* Sort the elment using InsertionSort */

printf("\nSort The Number Using InsertionSort\n");
 Inser_Sort(q, a);
/* Display the Sorted numbers */
printf("\nThe Sorted Number Are:");
for(i = 0;i < q;i++)
    printf("\ni = %d \t a = %d", i + 1, a[i]);

}

void Inser_Sort(int q,int a[])
{
 int i,j,k,temp;

for(j = 1; j < q; j++)
{
  temp = a[j];
for(k = j-1; k >= 0 && temp < a[k]; k--)
{
  a[k+1] = a[k];
}

a[k+1] = temp;

printf("\nIteration j = %d ", j);
for(i = 0; i < q; i++)
  printf("\ni = %d \t a = %d ", i+1,a[i]);
  printf("\n");
}
return;
}









C LANGUAGE BOOK

No comments:

Post a Comment