mercredi 5 août 2015

Bubble sort concept


I just want the explanation about the condition in nested for loop. How its processing, i am bit confused. I want the logic behind the conditions of two for loops.

 #include<stdio.h>
void bubblesort(int a[25],int n);

int main()
{
    int a[25],size,i;
    printf("Enter the size of an array");
    scanf("%d",&size);
    for(i=0;i<size;i++)
    {
        scanf("%d",&a[i]);
    }
    bubblesort(a,size);
    for(i=0;i<size;i++)
    {
        printf("%d\t",a[i]);
    }

    return 0;
}

void bubblesort(int a[], int n)
{

    int temp,i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<(n-i)-1;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]= temp;
            }
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire