0% found this document useful (0 votes)
9 views

Practical Number 6

Uploaded by

17.vikashverma
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 views

Practical Number 6

Uploaded by

17.vikashverma
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/ 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