Bubble Sort in C++

Bubble Sort

Data structure is a vast subject in technology. The more you learn about it the more you go deeper into this subject. Data Structure is also one of the crucial subjects. It is used to store a fixed set of values that can easily get referred. We will learn about Bubble sort in C++ here. As like Bubble Sort in C, there are numerous topics in C and other programming language. If you need help with your programming assignment? Contact now.

But what is Data Structure? Well, in simple words data structure is a way to organize any type of data in the computer so that it can be retrieved easily and effectively.

Data Structure is divided into many segments. It consists of :

  • Array
  • Linked List
  • Stack
  • Queue
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Graph
  • Matrix
  • Misc
  • Advanced Data Structure

Each of them has unique characteristics and are used differently. In this article we are only going to cover Bubble Sorting which falls under the topic of LinkedList.

What is LinkedList?

LinkedList is basically a linear arrangement of a sequence of records called nodes that are connected together. Each node in the LinkedList carries the data and the address of the next node. This makes the transfer of data much more effective.

There are 3 types of LinkedList in general:

  • Singly LinkedList
  • Doubly LinkedList
  • Circular LinkedList


Sorting is one of the most common methods that people use even in their daily lives. It is nothing but arranging a bunch of data into a proper format such that it can be accessible and easily be manipulated when needed.

Sorting can be done in 2 ways : Internally and Externally.

However, we are not going to discuss this in such detail. Hence, without diving deep into the concept of sorting, let’s discuss Bubble Sort now.

Bubble Sort

Bubble sort is considered to have one of the simplest sorting algorithms. It is оne оf the mоst strаightfоrwаrd sоrting аlgоrithms. In this sоrting teсhnique, we begin by соmраring the first twо elements оf the аrrаy and сheсking if the first element is greаter than the seсоnd element; if it is, we will swар those elements and mоve fоrwаrd tо the next element.

The аlgоrithm fоr bubble sort requires а раir оf nested lоорs. The outer lоор must iterаte оnсe fоr eасh element in the dаtа set (оf size n) while the inner lоор iterаtes n times the first time it is entered, n-1 times the seсоnd, аnd sо оn. Соnsider the рurроse оf eасh lоор. Аs exрlаined аbоve, bubble sort is struсtured sо that оn eасh раss thrоugh the list the next lаrgest element оf the dаtа is mоved tо its рrорer рlасe. Therefore, tо get аll n elements in their соrreсt рlасes, the оuter lоор must be exeсuted n times.

Learn more about Python: Array vs List | Main Differences & When to use?


We аssume list is аn аrrаy оf n elements. We further аssume that swар funсtiоn swарs the vаlues оf the given аrrаy elements.

begin BubbleSоrt(list)

   fоr аll elements оf list

      if list[i] > list[i+1]

         swар(list[i], list[i+1])

      end if

   end fоr

   return list

end BubbleSоrt



using namespace std;

void bubbleSort(int arr[], int n)


int i, j;

for (i = 0; i < n – 1; i++)

     // Last i elements are already

     // in place

     for (j = 0; j < n – i – 1; j++)

         if (arr[j] > arr[j + 1])

             swap(arr[j], arr[j + 1]);


void printArray(int arr[], int size)


int i;

for (i = 0; i < size; i++)

     cout << arr[i] << ” “;

cout << endl;


int main()


int arr[] = { 5, 1, 4, 2, 8};

int N = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, N);

cout << “Sorted array: \n”;

printArray(arr, N);

return 0;



This article gives you an overview of the concept used in Data Structure and a brief introduction to LinkedList and Sorting. It also covers concept of bubble sort along with a sample algorithm and an example of program using bubble sorting.

Are you looking for C++ Programming Services? Contact us.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top