• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Bubble sort in C
Next →
← Prev

What is bubble sort in C with example?

By Dinesh Thakur

Bubble sort in C is the most straightforward sorting algorithm called a sinking sort, and It works by repeatedly moving the largest elements to the highest index position in the array (if elements are to arranged in ascending order). It requires (n-1) passes to sort an array. For this, it uses several passes through the array and in each pass, the largest element searches its proper position in the sorted array. Bubble sort is not difficult to implement, and it’s fast enough once you have small data sets. The bubble sort algorithm has the same efficiency as the selection sort algorithm. 

In Bubble Sort, the first pass starts by comparing the adjacent pair of elements in the array starting at one end and swap (interchange) them if they are not in the proper order (i.e. first and second elements compared and swapped if first is greater than the second). We then move to the next higher position element and repeat this process (i.e. second and third elements are compared and swapped if necessary and so on). This process of comparing and interchanging repeated until the largest element bubbled from its original position to the correct place in the final sorted array. Every time the algorithm moves through the list, it’s known as a ‘pass.’

For sorting an array in ascending order, the first iteration moves the largest value to the last position of the array. As a result, the sufficient size of the array is reduced by 1. In the next pass, the process is repeated for new subarray (i.e. array with 1 less element than the original) until the largest element of this subarray bubbled to its correct position in the array (i.e. highest index position – 1 ). In subsequent passes, this process repeated until the effective size becomes 1, and in that case, the array sorted as all elements reach their respective positions.

bubble sort in CThe bubble sort gets its name because elements tend to move up into the appropriate order like bubbles rising to the surface.

We’ll be covering the following topics in this tutorial:

  • Time Complexity of Bubble Sort
  • Pseudocode for Bubble Sort
  • Bubble Sort Algorithm in C
  • Implementing the bubble sort program in C
  • How can I improve my bubble sort in C?
  • Optimised Bubble sort

Time Complexity of Bubble Sort

The order of growth of the bubble sort algorithm is Quadratic. Bubble sort has a worst-case and average complexity of О(n2) and will only run in its best-case functioning time of O(n) if the list already sorted (best-case), in which n is the number of items sorted. Bubble sort is a stable sort using a space complexity of O(1). Most functional sorting algorithms have considerably better worst-case or average complexity, frequently O(n log n).

The main drawback of bubble sort is time complexity. It’s the slowest algorithm, and it functions with a time complexity of O(n2). Its efficacy declines dramatically when the number of elements in the unsorted list rises.

Pseudocode for Bubble Sort

Here is the pseudo-code of bubble sort.

for i = 0 to n - 1 
  // to keep track of the number of iterations
  for j = 0 to n - 1 
    // to compare the elements inside the particular iteration
    // swap if any element is greater than its adjacent element
    if arr[j] < arr[j + 1] then 
      swap(arr[j], arr[j + 1])
    end if
  end for
end for

Bubble Sort Algorithm in C

• Start at index zero, compare the element with another one (a[0] & a[1]), also swap if a[0] > a[1]. Compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this procedure till the end of the array. After doing so, the biggest element is present in conclusion. This entire issue refers to a pass. In the first pass, we procedure array elements from [0,n-1].
• Repeat step one, however procedure array elements [0, n-2] because the previous one, i.e., a[n-1], is present in its proper position. Following this step, the most prominent two elements are found in the end.
• Repeat this procedure n-1 times.

Example:

First Pass:

( 6 2 5 3 9 ) → ( 2 6 5 3 9 ), Here, algorithm compares the first two elements, and swaps since 6 > 2.
( 2 6 5 3 9 ) → ( 2 5 6 3 9 ), Swap since 6 > 5
( 2 4 6 3 9 ) → ( 2 5 3 6 9 ), Swap since 6 > 3
( 2 5 3 6 9 ) → ( 2 5 3 6 9 ), Now, because all elements are already in order (9 > 6), algorithm does not swap them.

Second Pass:

( 2 5 3 6 9 ) → ( 2 5 3 6 9 )
( 2 5 3 6 9 ) → ( 2 3 5 6 ), Swap since 5 > 3
( 2 3 5 6 9 ) → ( 2 3 5 6 9 )
( 2 3 5 6 9 ) → ( 2 3 5 6 9 )
Now, the array already sorted, but our algorithm doesn’t know whether it’s complete. The algorithm requires one full pass with no swap to understand it’s sort.

Third Pass:

( 2 3 5 6 9 ) → ( 2 3 5 6 9 )
( 2 3 5 6 9 ) → ( 2 3 5 6 9 )
( 2 3 5 6 9 ) → ( 2 3 5 6 9 )
( 2 3 5 6 9 ) → ( 2 3 5 6 9 )

In the next program, we’re implementing bubble sort in C language. Within this program, the user would request to put in the number of elements and the element values. Then the program could sort them in ascending order with a bubble sorting algorithm.

Implementing the bubble sort program in C

Here is an example:

(7 5 3 2 4)

When You implement bubble sort, above array will be Flipped to (2 3 4 5 7)

Here is the example of c program for bubble sort

 
int main() {
 int array[100], n, i, j, swap; 
 printf("Enter number of elements n : ");
 scanf("%d", &n); 
 printf("Enter %d Numbers n :", n); 
 for(i = 0; i < n; i++)
   scanf("%d", &array[i]); 
   for(i = 0 ; i < n - 1; i++) {
     for(j = 0 ; j < n-i-1; j++) {
      if(array[j] > array[j+1]) {
         swap = array[j];
         array[j] = array[j+1];
         array[j+1] = swap;
       }
    }
} 
printf("Sorted Array:n"); 
 for(i = 0; i < n; i++)
   printf("%d", array[i]);
    return 0;
}

Output :

bubble sort program in cThis program provides you a demonstration of the bubble sort algorithm. In the first portion of the code, we take the number of elements in the array and save it. In the following section, the user enters the elements of the array.

Then there are two ‘for loops.’ The first ‘for loop’ runs from i value equal to zero until it’s significantly less than n-1. The outer array cares for the element of the value compared with the other elements.

There’s another loop within the loop, which begins from j = 0 to j<n-i-1. This loop takes care of all elements. Inside, there is an if statement that compares if a[j]>a[j+1]. Whether this condition fulfills, then swapping happens. A variable called swap use for this. To begin with, a[j] is assigned to swap, followed with a[j+1] assign at a[j], and last, swap assign into a[j+1]. It continues till all the elements sort—next, the sorted array print.

How can I improve my bubble sort in C?

An improved version of bubble sort, called modified bubble sort, includes a flag that set if an exchange made following an entire pass within the array. If no exchange creates, then it should be evident that the array is already in order because no two elements will need to be changed. If so, the sort ought to end. The new best circumstance order with this algorithm is O(n), as though the array already sorted; subsequently, no exchanges made.

Optimised Bubble sort

Optimized Bubble sort is among the most straightforward sorting methods. Possibly the only advantage it has over other approaches is detecting whether the input is already sorted. It is faster than a different sorted array and consumes less time to describe whether the input array sort or not.

You’ll also like:

  1. What is bubble sort in Java with example?
  2. What is Bubble Sort
  3. Array C++ Bubble sort
  4. C Program sorting of an int array using Bubble Sort
  5. Bubble memory – What is Bubble memory?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW