0% found this document useful (0 votes)
9 views5 pages

Bubble Sort Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Bubble Sort Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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