C++ Bubble sort Examples




C++ Bubble sort Examples

#include <iostream>

using namespace std;
 
int main(){

  int array[5];
  cout<<"Enter 5 numbers randomly : "<<endl;
  for(int i=0; i<5; i++)
  {

    cin>>array[i];
  }
  cout<<endl;
  cout<<"Input array is: "<<endl;
 
  for(int j=0; j<5; j++)
  {

    cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
  }
  cout<<endl;

  int temp;
  for(int i2=0; i2<=4; i2++)
  {
    for(int j=0; j<4; j++)
    {

      if(array[j]>array[j+1])
      {
        temp=array[j];
        array[j]=array[j+1];
        array[j+1]=temp;
      }
    }
  }

  cout<<"  Sorted Array is: "<<endl;
  for(int i3=0; i3<5; i3++)
  {
    cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl;
  }
  return 0;
}

Output

Enter 5 numbers randomly :
Input array is:
Value at 0 Index: 4545
Value at 1 Index: 1184
Value at 2 Index: 0
Value at 3 Index: 0
Value at 4 Index: 4197008
Sorted Array is:
Value at 0 Index: 0
Value at 1 Index: 0
Value at 2 Index: 1184
Value at 3 Index: 4545
Value at 4 Index: 4197008