# C++ Programming Code Examples

## C++ > Sorting Searching Code Examples

### C++ Program to Implement Bubble Sort

``` C++ Program to Implement Bubble Sort - Bubble sort algorithm sort data by comparing two consecutive numbers. - The time complexity of this algorithm is O(n^2). - Compare two consecutive number. - Switch values if the number with higher index value is smaller. - Display the result. - Exit. #include <iostream> using namespace std; // Sort arr[] of size n using Bubble Sort. void BubbleSort (int arr[], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n-i-1; ++j) { // Comparing consecutive data and switching values if value at j > j+1. if (arr[j] > arr[j+1]) { arr[j] = arr[j]+arr[j+1]; arr[j+1] = arr[j]-arr[j + 1]; arr[j] = arr[j]-arr[j + 1]; } } // Value at n-i-1 will be maximum of all the values below this index. } } int main() { int n, i; cout<<"\nEnter the number of data element to be sorted: "; cin>>n; int arr[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>arr[i]; } BubbleSort(arr, n); // Display the sorted data. cout<<"\nSorted Data "; for (i = 0; i < n; i++) cout<<"->"<<arr[i]; return 0; } ```