Bubble Sort Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

01 BUBBLE SORT 11.03.

24

AIM:

To design a C++ program to sort an array using Bubble Sort algorithm.

ALGORITHM:

STEP 1: Begin BubbleSort


STEP 2: For all possible iterations do
STEP 3: For all array elements do
STEP 4: If current array element is greater than the next array element then swap them
STEP 5: End if.
STEP 6: End for.
STEP 7: End for.
STEP 8: End BubbleSort
STEP 9: Stop
SOURCE CODE:

#include<iostream>

using namespace std;

int n;

void bubblesort(int arr[])


{
for(int i=1;i<=n-1;i++)
{
for(int j=0;j<n-i;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}

int main()
{
cout<<"Enter the number of Array Elements: ";
cin>>n;
cout<<endl;
int arr[n];
cout<<"Enter "<<n<<" Array Elements: "<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Unsorted Array Elements: ";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"Sorted Array Elements: ";
bubblesort(arr);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
INPUT AND OUTPUT:

RESULT:

Thus the given array is sorted using Bubble Sort algorithm in C++ language.

You might also like