Sorting array elements with bubble sort | Without Using Function | Using Function | Pointer | C-program

Bubble Sort:

Tips: 

  1. Use for loop.
  2. Use if and else.
  3. Do swapping.
  4. Bubble sort is done from starting of array.  

Without Function:

Code:

#include<stdio.h>

void main()

{
    int i,j,k,a[]={9,2,7,3};

    for(j=0;j<=3;j++)

        for(i=0;i<3;i++)

        {
            if(a[i]>a[i+1])

            {
               k=a[i];

               a[i]=a[i+1];

               a[i+1]=k;
               
            }

        }

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

    getch();

}

Using function

Code:

#include<stdio.h>

void main()

{

    int i,a[]={9,6,7,3},n;

    sort(a,3);

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

    getch();

}

void sort(int a[],int n)

{

    int i,j,k;

    for(j=0;j<=n;j++)

        for(i=0;i<=n;i++)

        {
            if(a[i]>a[i+1])

            {
                k=a[i];

                a[i]=a[i+1];

                a[i+1]=k;
            }

        }
}

Using Pointer

Code:

#include<stdio.h>

void main()

{
    int i,a[]={9,6,7,3},n;

    sort(a,3);

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

    getch();

}

void sort(int *p,int n)

{
    int i,j,k;

    for(j=0;j<=n;j++)

        for(i=0;i<=n;i++)

        {
            if(*(p+i)>*(p+i+1))

            {
                k=*(p+i);

                *(p+i)=*(p+i+1);

                *(p+i+1)=k;
            }

        }
}

Output:

Comments

Post a Comment

Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :