C Program to Sort an Integer Array Using Bubble Sort Algorithm

C program demonstrating Bubble Sort algorithm to sort integer arrays in ascending order, ideal for learning sorting basics in standard C environments.

Brief Introduction

C Program to Sort an Integer Array Using Bubble Sort Algorithm (c)
DESCRIPTION:
This program implements the Bubble Sort algorithm to sort an integer array in ascending order. The code is complete, commented in Italian, and ready to use in any standard C environment.

ADDITIONAL NOTES:
The Bubble Sort algorithm is simple to understand and implement but is not efficient for large datasets. This example is ideal for educational purposes and to understand the basic concepts of sorting.

EXPLANATIONS:
- The bubbleSort function takes as input an integer array and its size, and sorts the array in place using the Bubble Sort algorithm.
- The algorithm compares adjacent pairs of elements and swaps them if they are in the wrong order, repeating this process until the array is sorted.
- The printArray function prints the elements of the array on a single line, separated by spaces.
- In main, an example array is defined, printed before and after sorting to show the result.

Example Code: c

#include <stdio.h>

// Function to sort an integer array using Bubble Sort
void bubbleSort(int arr[], int n) {
    int i, j, temp;
    // Outer loop for each element of the array
    for (i = 0; i < n - 1; i++) {
        // Inner loop to compare adjacent elements
        for (j = 0; j < n - i - 1; j++) {
            // If the current element is greater than the next, swap them
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// Function to print the elements of the array
void printArray(int arr[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    // Definition of an unsorted integer array
    int array[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(array) / sizeof(array[0]);

    printf("Original array: \n");
    printArray(array, n);

    // Sorting the array
    bubbleSort(array, n);

    printf("Sorted array: \n");
    printArray(array, n);

    return 0;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Software: Standard C language (compatible with C99 and later).

Software: No external libraries required, uses only stdio.h for input/output.

Configuration: No special configuration required.

Hardware: No specific hardware required, can run on any PC or microcontroller with a C compiler.

Arduino: If you want to use it on Arduino, connect the device to the PC via USB to upload the program (no specific pins are needed for this example).

Similar Topics

Sum of two integers entered from keyboard
C program that reads two integers from keyboard, calculates their sum, and prints the result using standard input output functions for beginners.
Program that prints numbers from 1 to 1
This C program demonstrates printing numbers from 1 to 1 using a for loop, ideal for beginners learning basic C programming concepts.
Calculation of the average of three integers
C program that reads three integers from keyboard, calculates their average using float division, and prints the result with two decimal places.
Counting Characters in a String Without Using strlen
Learn how to count characters in a C string manually without using strlen by iterating until the terminator for better understanding of string handling.
Program that reads a character and displays its ASCII code
This C program reads a single character from the user and displays its ASCII code using getchar and printf functions in a simple beginner-friendly way.
Swapping the values of two integer variables
Complete C example demonstrating how to swap two integer variables using a temporary variable with pointers and clear Italian comments.
Reading and Printing an Array of 5 Integers
C program that reads and prints an array of 5 integers using loops and arrays, ideal for beginners learning basic input output in C programming.
Search for a Number in a Small Array (Linear Search)
Simple C program demonstrating linear search in a small integer array to find a number and print the result for beginners.