Index « Previous Next »

Question

Write a program to sort a list of numbers using bubble sort.

Source Code

#include <stdio.h>

void accept(int Arr[], int s);
void display(int Arr[], int s);
void bsort(int Arr[], int s);

int main()
{
    int list[100], n;
    printf("Enter Size of Array : ");
    scanf("%d", &n);
    accept(list, n);
    printf("\nBefore sorting");
    display(list, n);
    bsort(list, n);
    printf("\nAfter sorting");
    display(list, n);

    return 0;
}

void accept(int Arr[], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        printf("Enter element %d : ", i + 1);
        scanf("%d", &Arr[i]);
    }
}

void display(int Arr[], int s)
{
    int i;
    printf("\n");
    for (i = 0; i < s; i++)
    {
        printf("%d ", Arr[i]);
    }
}

void bsort(int Arr[], int s)
{
    int i, j, temp;
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1 - i); j++)
        {
            if (Arr[j] > Arr[j + 1])
            {
                temp = Arr[j];
                Arr[j] = Arr[j + 1];
                Arr[j + 1] = temp;
            }
        }
    }
}

Output

Enter Size of Array : 5
Enter element 1 : 15
Enter element 2 : 6
Enter element 3 : 9
Enter element 4 : 3
Enter element 5 : 11

Before sorting
15 6 9 3 11
After sorting
3 6 9 11 15