Q:

C Program to Sort n Numbers using Bubble Sort

0

Write a C Program to Sort n Numbers using Bubble Sort. Here’s simple C Program to Sort n Numbers using Bubble Sort in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. Here we need to sort a number in ascending order.

 
 

Below is the source code for C Program to Sort n Numbers using Bubble Sort which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program to Sort n Numbers using Bubble Sort  */

#include <stdio.h>
#define MAXSIZE 10
 
void main()
{
    int array[MAXSIZE];
    int i, j, num, temp;
 
    printf("Enter the value of num \n");
    scanf("%d", &num);
    printf("Enter the elements one by one \n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("Input array is \n");
    for (i = 0; i < num; i++)
    {
        printf("%d\n", array[i]);
    }
    /*   Bubble sorting begins */
    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("Sorted array is...\n");
    for (i = 0; i < num; i++)
    {
        printf("%d\n", array[i]);
    }
}

Output:


/*  C Program to Sort n Numbers using Bubble Sort  */

Enter the value of num
6
Enter the elements one by one
2
4
6
8
1
3
Input array is
2
4
6
8
1
3
Sorted array is...
1
2
3
4
6
8

Above is the source code for C Program to Sort n Numbers using Bubble Sort which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to Sort Array in Ascending Order... >>
<< C Program to Sort Numbers using Selection sort...