CrazyEngineers
  • Bubble sort help plz

    Aman Goyal

    Member

    Updated: Oct 25, 2024
    Views: 1.4K
    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;
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • PraveenKumar Purushothaman

    MemberJun 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]);  
    }
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJun 27, 2011

    Sorry for double posting... 😔
    Are you sure? This action cannot be undone.
    Cancel
  • Aman Goyal

    MemberJun 27, 2011

    thanku Praveen but plz do find the bug in my code,i think my logic is right
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJun 27, 2011

    What output are you getting?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register