Showing posts with label Array Bubble sort. Show all posts
Showing posts with label Array Bubble sort. Show all posts

Monday, September 12, 2011

insert element in any array

main()
{
    int n,count=0,i,j,k,m,temp;
    int a[10];
    clrscr();
    printf("\n Enter number of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n Enter the element no. %d:",i+1);
      scanf("%d",&a[i]);
    }
    for(j=1;j<n;j++)
    {
      printf("\n Iteration number %d:-\n",j-1);
      for(m=0;m<n;m++)
        printf("%d\n",a[m]);

      for(i=0;i<j;i++)
        if(a[i]>a[j])
          {
        temp=a[j];
        for(k=j;k>i;k--)
          a[k]=a[k-1];

        a[k]=temp;
        break;
          }
       printf("\n");
       getch();
       }
      printf("last iteration is %d:-\n",j-1);
      for(i=0;i<n;i++)
     printf("%d\n",a[i]);
 getch();
 }

Array Bubble sort

-: Bubble Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=0;j<n-1;j++)
            if(a[j]<=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

Popular 5 Posts Last 7 days