
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Bubble Sort
Bubble Sort is comparison based sorting algorithm. In this algorithm adjacent elements are compared and swapped to make correct sequence. This algorithm is simpler than other algorithms, but it has some drawbacks also. This algorithm is not suitable for large number of data set. It takes much time to solve the sorting tasks.
The complexity of Bubble Sort Technique
Time Complexity: O(n) for best case, O(n2) for average and worst case
Space Complexity: O(1)
Input − A list of unsorted data: 56 98 78 12 30 51 Output − Array after Sorting: 12 30 51 56 78 98
Algorithm
bubbleSort(array, size)
Input: An array of data, and the total number in the array
Output: The sorted Array
Begin for i := 0 to size-1 do flag := 0; for j:= 0 to size –i – 1 do if array[j] > array[j+1] then swap array[j] with array[j+1] flag := 1 done if flag ≠ 1 then break the loop. done End
Example Code
#include<iostream> using namespace std; void swapping(int &a, int &b) { //swap the content of a and b int temp; temp = a; a = b; b = temp; } void display(int *array, int size) { for(int i = 0; i<size; i++) cout << array[i] << " "; cout << endl; } void bubbleSort(int *array, int size) { for(int i = 0; i<size; i++) { int swaps = 0; //flag to detect any swap is there or not for(int j = 0; j<size-i-1; j++) { if(array[j] > array[j+1]) { //when the current item is bigger than next swapping(array[j], array[j+1]); swaps = 1; //set swap flag } } if(!swaps) break; // No swap in this pass, so array is sorted } } int main() { int n; cout << "Enter the number of elements: "; cin >> n; int arr[n]; //create an array with given number of elements cout << "Enter elements:" << endl; for(int i = 0; i<n; i++) { cin >> arr[i]; } cout << "Array before Sorting: "; display(arr, n); bubbleSort(arr, n); cout << "Array after Sorting: "; display(arr, n); }
Output
Enter the number of elements: 6 Enter elements: 56 98 78 12 30 51 Array before Sorting: 56 98 78 12 30 51 Array after Sorting: 12 30 51 56 78 98
- Related Articles
- Java program to implement bubble sort
- Code to implement bubble sort - JavaScript
- Python Program for Bubble Sort
- Bubble Sort program in C#
- 8085 program for bubble sort
- Implement Bubble sort with negative and positive numbers – JavaScript?
- C++ Program for Recursive Bubble Sort?
- Java Program for Recursive Bubble Sort
- C Program for Recursive Bubble Sort
- 8085 Program to perform sorting using bubble sort
- Bubble Sort
- Write a Golang program to sort an array using Bubble Sort
- C++ Program for the Recursive Bubble Sort?
- 8085 Program to perform bubble sort in ascending order
- 8085 Program to perform bubble sort in descending order

Advertisements