Bubble Sort in C++
To sort an array in ascending order using bubble sort in C++ programming,
you have to ask to the user to enter the array size then ask to enter array elements,
now start sorting the array elements using the bubble sort technique and
display the sorted array on the screen as shown here in the following program.
#include <iostream>
using namespace std;int main()
{ // declaration int n, i, arr[10], j, temp;//
cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter "<<n<<" numbers :"; for(i=0; i<n; i++) { cin>>arr[i]; } cout<<"Sorting array using bubble sort technique...\n";// sorting
for(i=0; i<(n-1); i++) {// the next
for(j=0; j<(n-i-1); j++) { if(arr[j]>arr[j+1]) {// swap
temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } }//
cout<<"Elements sorted successfully..!!\n"; cout<<"Sorted list in ascending order :\n"; for(i=0; i<n; i++) { cout<<arr[i]<<" "; } return 0;}xfer from :
https://codescracker.com/cpp/program/cpp-program-bubble-sort.htm
#include <iostream>
using namespace std;main(){
//
int i,j;
int temp; int n=5; int a[5]={45,3,32,12,65}; // for(i = 0; i < n ; i++) for(j = n - 1 ; j > i ; j--){
//
if(a[j - 1] < a[j]){
//
temp = a[j - 1];
a[j - 1]=a[j]; a[j]=temp;}}//
for (int i=0;i<n;i++){//
cout<<a[i]<<" " ; } return 0;}