#include<iostream>usingnamespacestd;voidBubbleSort(inta[],intn){for(inti=0;i<(n-1);i++){for(intj=0;j<n-i-1;j++){if(a[j]>a[j+1]){a[j]=a[j]+a[j+1];a[j+1]=a[j]-a[j+1];a[j]=a[j]-a[j+1];}}}}intmain(){intn,i;cout<<"———————————————————————————————————————————";cout<<"\nImplementation of a Bubble Sort\n";cout<<"———————————————————————————————————————————";cout<<"\nEnter the number of elements ";cin>>n;inta[n];for(i=0;i<n;i++){cin>>a[i];}BubbleSort(a,n);cout<<"\nSorted elements ";for(i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n———————————————————————————————————————————\n";return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Implementation of a Bubble Sort
———————————————————————————————————————————
Enter the number of elements 6
12
1024
-2048
0
-1
95
Sorted elements -2048 -1 0 12 95 1024
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.