Bubble sort help plz

Aman Goyal

Aman Goyal

@aman-goyal-gQNNWx Oct 25, 2024
plz help,im getting incorrect output

#include<stdio.h>
int main()
{
int i,pass,temp,a[10],n;
printf("Enter the number of elements you would like to enter");
printf("%d",n);
printf("Enter the elements of the array");
for(i=0;i<n;i++)
scanf("%d",&a);
for(pass=1;pass<=n-1;pass++)
{
for(i=0;i<=n-1-pass;i++)
{
if(a>a[i+1])
{
temp=a;
a=a[i+1];
a[i+1]=temp;
}
}
}
printf("The sorted array is");
printf("%d",a);
return 0;
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92 Jun 27, 2011

    Try this...
    #include <stdio.h>  
    #include <conio.h>  
    
    void bubble(int a[],int n)  
    {  
        int i, j, t;  
        for(i = n-2;i >= 0;i--)  
        {  
            for(j=0;j<=i;j++)  
            {  
                if(a[j]>a[j+1])  
                {  
                    t=a[j];  
                    a[j]=a[j+1];  
                    a[j+1]=t;  
                }  
            }  
        }
    }
    
    void main()  
    {  
        int a[100],n,i;  
        clrscr();  
        printf("\n\n Enter integer value for total no.s of elements to be sorted: ");  
        scanf("%d",&n);  
        for( i=0;i<=n-1;i++)  
        {
            printf("\n\n Enter integer value for element no.%d : ",i+1);  
            scanf("%d",&a[i]);  
        }  
        bubble(a,n);  
        printf("\n\n Finally sorted array is: ");  
        for( i=0;i<=n-1;i++)  
        printf("%3d",a[i]);  
    }
  • PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92 Jun 27, 2011

    Sorry for double posting... 😔
  • Aman Goyal

    Aman Goyal

    @aman-goyal-gQNNWx Jun 27, 2011

    thanku Praveen but plz do find the bug in my code,i think my logic is right
  • PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92 Jun 27, 2011

    What output are you getting?