Practical Number 6

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

Practical Number: 6

Write a program to sort an array of integers in ascending order using Bubble


sort.
Sol: Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.

#include<iostream>
using namespace std;

void bubbleSort(int arr[], int n){


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

int main(){
int n;
cout<<"Enter number of element in array : ";
cin>>n;

int arr[n];

16
cout<<endl<<"Enter element in array : ";
for(int i=0; i<n; i++){
cin>>arr[i];
}

bubbleSort(arr,n);
cout<<endl<<"Array after sorting : ";
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}

return 0;
}
Output:

17

You might also like