C++ Programming Code Examples C++ > Sorting Searching Code Examples Simple Bubble Sort Program in C++ Simple Bubble Sort Program in C++ Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position. #include <iostream> #include<conio.h> #include<stdlib.h> #define maxsize 5 using namespace std; int main() { int arr_sort[maxsize], i, j, a, t; cout << "Simple C++ Bubble Sort Example - Array\n"; cout << "\nEnter " << maxsize << " Elements for Sorting : " << endl; for (i = 0; i < maxsize; i++) cin >> arr_sort[i]; cout << "\nYour Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } for (i = 1; i < maxsize; i++) { for (j = 0; j < maxsize - 1; j++) { if (arr_sort[j] > arr_sort[j + 1]) { //Swapping Values t = arr_sort[j]; arr_sort[j] = arr_sort[j + 1]; arr_sort[j + 1] = t; } } cout << "\nIteration : " << i; for (a = 0; a < maxsize; a++) { cout << "\t" << arr_sort[a]; } } cout << "\n\nSorted Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } }