Bubble sort using ‘c

Bubble sort is a basic arranging calculation that more than once steps through a rundown of components, looks at contiguous coordinates, and trades them assuming they are all mixed up. It gets its name from the way more modest components "bubble" to the first spot on the list during every cycle.


Here is a bit by bit portrayal of the air pocket sort calculation:


1. Begin toward the start of the rundown.


2. Contrast the main component and the subsequent component.


3. Assuming the primary component is more noteworthy than the subsequent component, trade their positions.


4. Move to the following sets of components (second and third) and rehash stage 3.


5. Proceed with this cycle, contrasting and trading neighboring components until arriving at the finish of the rundown.


6. As of now, the biggest component will have "rose" to the furthest limit of the rundown.


7. Rehash stages 2 to 6 for the leftover components, barring the last component that is as of now in its right arranged position.


8. After every cycle, the biggest unsorted component will move to the furthest limit of the rundown.


9. Rehash the interaction until the whole rundown is arranged.


Bubble sort is known for its effortlessness however isn't exceptionally proficient for enormous records or clusters. Its normal and most pessimistic scenario time intricacy is O(n^2), where n is the quantity of components in the rundown. This implies that it might require countless correlations and trades, making it less proficient contrasted with further developed arranging calculations like consolidation sort or quicksort. Notwithstanding, it tends to be valuable for little records or as a training instrument to figure out arranging ideas.

Program

 

#include <stdio.h>

#include <stdlib.h>

int main()

{

    int a[50],num,x,y,temp,i,j,n;

    printf("no of elements  u want ");

    scanf("%d",&n);

    printf("enter the values");

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

    {

        scanf("%d",&a[i]);

 

    }

    merge(a,0,n-1);

    printf("sorted array is");

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

        printf("%d\t",a[i]);

return 0;

}

 

int merge(int a[],int beg,int end)

{

    int mid;

    if(beg<end)

    {

        mid=(beg+end)/2;

        merge(a,beg,mid);

        merge(a,mid+1,end);

        sort(a,beg,mid,end);

    }

}

int sort(int a[],int beg,int mid,int end)

{

    int i=beg,j=mid+1,index=beg,temp[100],k;

    while((i<=mid)&&(j<=end))

    {

        if(a[i]<a[j])

        {

            temp[index]=a[i];

            i++;

        }

        else

        {

            temp[index]=a[j];

            j++;

        }

        index++;

    }

    if(i>mid)

    {

        while(j<=end)

        {

            temp[index]=a[j];

            j++;

            index;

        }

    }else

    {

 

    while(i<=mid)

    {

        temp[index]=a[i];

        i++;

        index++;

    }

    }

    for(k=beg;k<index;k++)

        a[k]=temp[k];

}

Output