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 array
//   and sort these numbers in ascending order
// using  Bubble sort algorithm
// Author: www.EasyCodeBook.com (c)
#include<iostream>
#include<conio.h>
using namespace std;
 main()
{
	int arr[10],i,temp,last;
	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(last=9;last>=1;last--)
	for(i=0;i<=last-1;i++)
		if (arr[i]>arr[i+1])
		{
			temp=arr[i];
			arr[i]=arr[i+1];
			arr[i+1]=temp;		
		}
	cout<<"\nArray Values after sorting in ascending order using Bubble Sort Algorithm:"<<endl;
	for (i=0;i<10;i++)
	cout<<arr[i]<<", ";
}

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,

Leave a Reply

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