Basic Bubble Sort Code in C++

By | July 11, 2019

Task: Basic Bubble Sort Code in C++

// Write a C++ program to input 10 numbers in arra
//   and sort these numbers in ascending orde
// using  Bubble sort algorith
// Author: www.EasyCodeBook.com (c
#include<iostream>
#include<conio.h>
using namespace std;
 int main()
{
	int arr[10],i,j, temp;
	for(i=0;i<10;i++)
	{
		cout<<"Enter Number "<<(i+1)<<":";
		cin>>arr[i];
	}
	cout<<"Array Values before sorting:"<<endl;
	for(i=0;i<10;i++)
	cout<<arr[i]<<", ";


	for(i=0;i<10;i++)
	for(j=0;j<10-i-1;j++)
		if (arr[j]>arr[j+1])
		{
			temp=arr[j];
			arr[j]=arr[j+1];
			arr[j+1]=temp;	
		}
	cout<<"\nArray Values after sorting in ascending order using Bubble Sort Algorithm:"<<endl;
	for (i=0;i<10;i++)
	cout<<arr[i]<<", ";
    return 0;


}

The output of Simple Bubble Sort C++ Program

Enter Number 1:34
Enter Number 2:-1
Enter Number 3:-2
Enter Number 4:45
Enter Number 5:67
Enter Number 6:8
Enter Number 7:123
Enter Number 8:456
Enter Number 9:6
Enter Number 10:6
Array Values before sorting:
34, -1, -2, 45, 67, 8, 123, 456, 6, 6,
Array Values after sorting in ascending order using Bubble Sort Algorithm:
-2, -1, 6, 6, 8, 34, 45, 67, 123, 456,

Output 2

Enter Number 1:3
Enter Number 2:4
Enter Number 3:55
Enter Number 4:67
Enter Number 5:89
Enter Number 6:0
Enter Number 7:12
Enter Number 8:3
Enter Number 9:4
Enter Number 10:5
Array Values before sorting:
3, 4, 55, 67, 89, 0, 12, 3, 4, 5,
Array Values after sorting in ascending order using Bubble Sort Algorithm:
3, 4, 55, 67, 89, 0, 12, 3, 4, 5,

Leave a Reply

Your email address will not be published. Required fields are marked *