๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.

โ† All Lab Programs

Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.

Bubble sort
C Program
//The Bubble sort method to sort a given list of integers in ascending order.
#include <stdio.h>

int main() {
    int arr[50], n, i;
    void bubbleSort(int [], int );

    // Input number of elements
    printf("Enter number of elements: ");
    scanf("%d", &n);

    // Input elements
    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Call bubble sort function
    bubbleSort(arr, n);

    // Print sorted array
    printf("Sorted list in ascending order:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

// Bubble sort function
void bubbleSort(int arr[], int n) {
    int i, j, temp;

    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

OUTPUT
Enter number of elements: 9
Enter 9 integers:
4 5 6 1 7 2 8 3 9
Sorted list in ascending order:
1 2 3 4 5 6 7 8 9 

๐Ÿ’ป Run This C Program

Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals