Bubble Sort Program in C2 Apr 2025 | 4 min read Bubble sort is a simple and intuitive sorting algorithm. It repeatedly swaps adjacent elements if they are in the wrong order until the array is sorted. In this algorithm, the largest element "bubbles up" to the end of the array in each iteration. Bubble sort is inefficient for large data sets, but it is useful for educational purposes and small data sets. In this article, we will implement the bubble sort algorithm in C programming language. The first step is to define the bubble sort function. This function takes an integer array and the size of the array as its parameters. The function returns nothing as it modifies the original array. Here is the function definition: The function has two loops. The outer loop runs from the first element to the second-last element of the array. The inner loop runs from the first element to the second-last element of the unsorted part of the array. The condition of the inner loop is n - i - 1 because the last i elements of the array are already sorted. In each iteration of the inner loop, we compare adjacent elements. If the left element is greater than the right element, we swap them. After the inner loop completes, the largest element is guaranteed to be at the end of the unsorted part of the array. Now, we can write the main function to test our bubble sort implementation. Here is the main function along with the previous part: C Program: The main function creates an integer array arr of size 7 and initializes it with random numbers. We then calculate the size of the array by dividing the size of the array by the size of an integer element. Next, we call the bubble_sort function to sort the array. Finally, we print the sorted array using a for loop. When we run the program, we should see the following output: Sorted array: 11 12 22 25 34 64 90 This output shows that our bubble sort implementation correctly sorted the array in ascending order. To run the program, we need to compile it using a C compiler. Here is an example compilation command for GCC: This command compiles the bubble_sort.c file and produces an executable file named bubble_sort. In summary, the bubble sort algorithm repeatedly swaps adjacent elements until the array is sorted. The algorithm has a time complexity of O(n2), which makes it inefficient for large data sets. However, it is useful for educational purposes and small data sets. We implemented the bubble sort algorithm in C programming language and tested it using a simple example. Characteristics:
Usage:
Advantages:
Disadvantages:
Conclusion:Bubble sort is a simple and intuitive sorting algorithm that is useful for educational purposes and small data sets. However, its time complexity makes it inefficient for large data sets. Therefore, it is not commonly used in real-world applications. Other sorting algorithms, such as quicksort and mergesort, are more efficient for large data sets. Next TopicComma Operator in C |
The C string manipulation function "strtok()" is used to split a string into tokens or smaller strings based on a designated delimiter. The "string.h" header file contains the function's declaration. Syntax: Syntax for using strtok() function is as follows: Char *strtok(char *str,const char *delim); The string that is to be...
3 min read
The length of the characters before the first character contained in both strings is determined using the C library function strcspn(). Syntax: strcspn(const char *str1, const char *str2) Parameters or arguments used in this function: str1: The string that must be searched for, or the target string. str2: Characters from the argument string...
3 min read
In computer programming, arrays are a fundamental type of data structure that lets us efficiently store and manage data. They are a group of related data elements that are kept in close proximity to one another in memory. The programming language C supports one-dimensional (1D) and...
4 min read
Matrices are widely used in various fields such as physics, engineering, and computer science. In C programming language, matrices are used to represent and manipulate multi-dimensional arrays of data. Here are a few examples of why we might need to use matrices in C: Image Processing: Matrices...
4 min read
? We declare variables in almost every program. Not every variable has the same features. The declarations, accessibility in different parts of the program varies from variable to variable depending on the location the variable is declared. "Storage classes" simply are used to determine some important features...
6 min read
In this article, we will discuss the reentrant function in C with its properties and examples. If there is a way to stop a function from running in the middle of it, handle the interrupt service routine, and then restart the earlier running function without impairing it,...
3 min read
In this article, you will learn about a project which demonstrates how to build a language. You will learn this concept through a single program which details all the functions happening in the entire process. What is School Billing System, and why should we use...
181 min read
SJF (Shortest Job First) is a scheduling strategy that gives the process with the quickest CPU burst time to the CPU first. As this technique is non-preemptive, once a process has begun to run, it cannot be stopped until it has finished. The SJF scheduling method...
6 min read
Now, we will look at how to count the number of digits in an integer. This integer is nothing but the number entered by the user. First, we will calculate count the number of digits using for or while loop. Approach Firstly, the number will be entered by the...
3 min read
Given a lower limit x and an upper limit y, if we're supposed to count the number of odd and even numbers in the range, we can opt for the regular method of using a for loop and iterating from the lower limit to the upper...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India