Ad Code

Responsive Advertisement

Bubble Sort using C language

 Input : 5 7 1 4 8

Output : 1 4 5 7 8


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void bubbleSort(int arr[], int n);
int main()
{
    int a[20], n, i;
    printf("Enter the array size : \n");
    scanf("%d", &n);
    printf("Enter the array elements : \n");
    for (i = 0; i < n; i++)
    {
        printf("Enter value of this index [%d] : ", i);
        scanf("%d", &a[i]);
    }
    bubbleSort(a, n); // calling Function
    printf("Array after implementing bubble sort: \n");
    for (int i = 0; i < n; i++)
    {
        printf("%d\t", a[i]);
    }
    getch();
    return 0;
}
void bubbleSort(int arr[], int n)
{
    int i, step, temp;
    for (step = 1; step < n; step++)
    {
        for (i = 0; i < n - step; i++)
        {
            if (arr[i] > arr[i + 1])
            {
                temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
    }
}

Post a Comment

0 Comments

Ad Code

Responsive Advertisement