firstly , we need to know about bubble short . what is happening during bubble shorting.

>>> In Bubble short elements of one position is compare to the next position and arrange the elements whatever we want.

Here is the source code example for bubble short.

void main()
 {
     int array[10];
     int i, j, num, temp;
printf("Enter number of elements  \n");
scanf("%d", &num);
printf("Enter the elements\n");
for(i = 0; i < num; i++)
{
    scanf("%d", &array[i]);
}
for (i = 0; i < num; i++)
{
    for (j = 0; j < (num - i - 1); j++)
    {
        if (array[j] > array[j + 1])
        {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}
printf("required shorted array is\n");
for (i = 0; i < num; i++)
{
    printf("%d\n", array[i]);
}
}

Output:

Explanation:

1.firstly , program ask user to input number of elements and ask for enter elements.

2.After getting the elements program start to compare all the elements inside the for loop.

using: if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
} this block.

3.And lastly print the required shorting elements.

Leave a Reply

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