Skip to main content

Bubble Sort Code in C: Simply Way to Learn Bubble Sort in C Language

Bubble Sort Code in C

If you are starting your journey into data structures and algorithms, the bubble sort code in c is often the first sorting technique you will encounter. While it might not be the fastest algorithm for massive datasets, its logic is the foundation for understanding how computers organise information.

Bubble Sort Code in C: Simply Way to Learn Bubble Sort in C Language

What is Bubble Sort?

Bubble sort is a simple, comparison based algorithm. It works by repeatedly stepping through a list, comparing adjacent elements and swapping them if they are in the wrong order. This process repeats until the largest elements "bubble up" to their correct positions at the end of the list.

Bubble Sort Code in C Using For Loop

The most common way to implement this is by using nested loops. The outer loop tracks the number of passes, while the inner loop handles the individual comparisons.

#include <stdio.h>

int main() {
    int data[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(data)/sizeof(data[0]);
    int i, j, temp;

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

    printf("Sorted array: \n");
    for (i = 0; i < n; i++)
        printf("%d ", data[i]);
    return 0;
}
    

Bubble Sort Program in C User Input

In real world scenarios, you often need to sort data provided by a user. Here is how you can write a bubble sort program in c user input style to make it interactive.

#include <stdio.h>

int main() {
    int a[100], n, i, j, swap;

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

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

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

    printf("Sorted list in ascending order: \n");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
    

Bubble Sort Code in C Without Function

Many students look for a bubble sort code in c without function calls to keep the logic contained entirely within the main block. The examples provided above demonstrate this approach, making it easier to follow the flow of variables without jumping between different sections of memory.

Comparing Bubble Sort with Other Algorithms

While bubble sort is great for learning, it is important to know about other fundamental sorting techniques like selection sort and insertion sort.

Selection Sort in C

Selection sort works by finding the minimum element from the unsorted part and putting it at the beginning. It generally performs fewer swaps than bubble sort.


Insertion Sort in C

Insertion sort builds the final sorted array one item at a time. It is much more efficient for small datasets or arrays that are already mostly sorted.



Bubble Sort Code in C with Example Walkthrough

Let us look at a bubble sort code in c with example values. If you have an array (5, 1, 4):

  • First Pass: Compare 5 and 1. Since 5 > 1, swap them. Array becomes (1, 5, 4).
  • Compare 5 and 4. Since 5 > 4, swap them. Array becomes (1, 4, 5).
  • The largest number 5 is now at the end.

Conclusion

Mastering the bubble sort code in c is a vital milestone for any programmer. It teaches the importance of loops, conditional logic, and array manipulation. Once you are comfortable with this, you can move on to more advanced algorithms like Quick Sort or Merge Sort.

Comments

Popular posts from this blog

Kotlin vs Swift 2025 Updated Information

Kotlin vs Swift Developers face a big choice in the mobile app world: Kotlin or Swift . Both are popular and offer unique benefits for Android and iOS apps. We'll examine what makes them different and similar, focusing on their use in projects and how they affect work and user experience. Kotlin is an open-source language by JetBrains , which is seen as a strong Java alternative for Android. It's known for its simple syntax, easy Java integration, and safety features. On the other hand, Swift is Apple's choice for iOS apps. It's modern, expressive, and safe. We'll compare Kotlin and Swift by looking at their basics, features, environments, performance, learning, and ecosystems. This deep dive will guide you in choosing the best language for your project, considering your team's skills and the 2024 mobile app scene. Understanding the Foundations of Mobile Development Languages Exploring mobile app development, we must grasp the roots of key programming languages...

Fruitful Functions In Python 2025

Discover Fruitful Functions in Python Python is a versatile programming language with many tools and techniques . One key feature is fruitful functions , which make your code more efficient and functional. We'll explore fruitful functions, their importance, structure, and how they're used in Python. Fruitful functions, or return functions , are essential in Python. They differ from void functions by returning values that can be used in your code. These functions can return one value, many values, or complex data structures. This makes them very useful for solving various programming problems. Learning about fruitful functions can help you write better code. It's useful for both new and experienced Python developers. Knowing how to use fruitful functions will improve your skills and help you solve complex problems easily. Understanding the Basics of Fruitful Functions in Python Fruitful functions in Python are key for any programmer to know. They differ from void functions...

Magic Number Program in Java

Magic Number Program in Java A magic number is an intriguing concept in programming that captures developers' curiosity. In Java, a magic number is a number that reduces to 1 after repeatedly summing the squares of its digits. If a number does not reduce to 1, it is not considered a magic number. This article will explore the concept of magic numbers, their significance, and how to implement a magic number program in Java. This guide will help beginners and seasoned developers understand and implement this program efficiently. What is a Magic Number? In mathematics, a magic number is a special number with unique properties. In programming, it is often used to identify numbers that meet specific conditions. For instance, a magic number is a number whose digits can be repeatedly squared and summed until the result is 1. If the result never reaches 1, the number is not magical. Example of a Magic Number Let’s take 19 as an example: Calculate the sum of the squares of its digits: Repea...