Data Structure Bubble Sort

program of bubble sorting is given below:

//bubblesort using array
#include<stdio.h>
void bubblesort(int *A,int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(*(A+i)>*(A+j))
            {
                temp =*(A+i);
                *(A+i)=*(A+j);
                *(A+j)=temp;
            }
        }
    }
}
void main()
{
    int B[50],n;
    printf("Enter the size of array :");
    scanf(" %d",&n);
    printf("Enter the array:\n");
    for(int i=0;i<n;i++)
    {
        scanf(" %d",&B[i]);
    }
    bubblesort(&B[0],n);
    printf("The sort array is :");
    for(int i=0;i<n;i++)
    {
        printf(" %d",B[i]);
    }
}

//bubblesort without using array
#include<stdio.h>
#include<stdlib.h>
void bubblesort(int *A,int n)
{
    int i,j,temp;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(*(A+i)>*(A+j))
            {
                temp =*(A+i);
                *(A+i)=*(A+j);
                *(A+j)=temp;
            }
        }
    }
}
void main()
{
    int *B,n;
    printf("Enter the size of array :");
    scanf(" %d",&n);
    B = (int*)malloc(n*sizeof(int));
    printf("Enter the array:\n");
    for(int i=0;i<n;i++)
    {
        scanf(" %d",(B+i));
    }
    bubblesort(B,n);
    printf("The sort array is :");
    for(int i=0;i<n;i++)
    {
        printf(" %d",*(B+i));
    }
}

Output

Enter the size of array :5
Enter the array:
5
4
10
1
6
The sort array is : 1 4 5 6 10

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin