Home

A program to explain BUBBLE SORT using pointers

C Programming Language 113

Program

#include <stdio.h>

void main()
{
    int a[30], n, i, j, t;

    printf("Enter how many numbers you want to input: ");
    scanf("%d", &n);

    printf("Enter numbers: ");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }

    printf("After sorting: ");

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

    printf("\n");
}

Result

Enter how many numbers you want to input: 7
Enter numbers: 3 4 9 5 7 2 34
After sorting: 2 3 4 5 7 9 34


Last Updated on

Next Post: Rules vs Guidelines →

Comments