Code Scrappers


Bubblesort C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    void main()

                    {

                        int  a[10], n, i, j, pass, temp;

                        printf("Enter the size of array:  ");

                        scanf("%d",&n);

                        printf("Enter the array elements:  ");


                        for(i=0;i<n;++i)
                        {
                            scanf("%d",&a[i]);
                        }

                        for(p=1; p<n; p++)
                        {
                              for(j=0;j<(n-p);++j)
                              {
                                       if(a[j] > a[j+1])

                                       {

                                          	temp = a[j];

                                          	a[j] = a[j+1];

                                          	a[j+1] = temp;

                                	     }
                        }      }

                        printf("\nArray after sorting: \n ");

                        for(i=0;i<n;++i)
                        {
                            printf("%d \n",a[i]);
                        }

                    }


                



Output

            Enter the size of array:  5
            Enter the array elements:
            1
            4
            2
            7
            8

            Array contents after sorting:
            1
            2
            4
            7
            8