You are on page 1of 2

Exp No: 6B BUBBLE SORT

Date:

AIM

To write a c++ program to implement bubble sort algorithm to sort the given numbers

ALGORITHM:

Step 1: Start the program

Step 2: Get the array of elements to be sorted

Step 3: Starting with the first element(index = 0), compare the current element with the
next element of the array.

Step 4: If the current element is greater than the next element of the array, swap them.

Step 5: If the current element is less than the next element, move to the next element

Step 6: Repeat step 3 to 6 for n-1 passes or until no swapping is done in a pass

Step 7: Display the sorted array

Step 8: Stop the program

PROGRAM:

#include<iostream>

using namespace std;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;

void bubbleSort(int *array, int size) {

for(int i = 0; i<size; i++) {


int swaps = 0;

for(int j = 0; j<size-i-1; j++) {

if(array[j] > array[j+1]) {

int temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;

swaps = 1;

if(!swaps)

break;

int main() {

int n=10;

int arr[]={23,12,56,34,89,90,5,21,77,11};

cout << "Array before Sorting: ";

cout<<arr[0];

display(arr, n);

bubbleSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

You might also like