void bubbleSort(vector<int>& arr, int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
swap(arr[i],arr[j]);
}
}
}
}
Problem of the day

You are given an integer array 'arr' of size 'N'.
You must sort this array using 'Bubble Sort'.
Change in the input array itself. You don't need to return or print the elements.
Example:
Input: ‘N’ = 7
'arr' = [2, 13, 4, 1, 3, 6, 28]
Output: [1 2 3 4 6 13 28]
Explanation: After applying bubble sort on the input array, the output is [1 2 3 4 6 13 28].
The first line contains an integer 'N' representing the size of the array.
The second line contains 'N' single space-separated integers representing the elements of the array.
Output format :
The output contains the array elements after the sorting.
7
2 13 4 1 3 6 28
1 2 3 4 6 13 28
After applying bubble sort on the input array, the output is [1 2 3 4 6 13 28].
5
9 3 6 2 0
0 2 3 6 9
After applying bubble sort on the input array, the output is [0 2 3 6 9].
1 <= N <= 10^3
0 <= arr[i] <= 10^9
Time Limit: 1 sec
Apply Bubble Sort.
The steps are as follows:-
// Function to sort an array using Bubble Sort
function bubbleSort(int[] arr, int n):
O( N ^ 2 ), where 'N' is the array ‘arr’ size.
In the worst case, we have to do n^2 iterations on the array ‘arr’,
Hence the time complexity is O( N ^ 2 ).
O( 1 ).
We are only some variables,
Hence the space complexity is O( 1 ).
Interview problems
Bubble sort in c++
void bubbleSort(vector<int>& arr, int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
swap(arr[i],arr[j]);
}
}
}
}
Interview problems
Bubble Sort | Easy C++ code
#include<bits/stdc++.h>
using namespace std;
void bubbleSort(vector<int>& arr, int n)
{
// Write your code here.
for(int i=n-1;i>=0;i--){
int didSwap=0;
for(int j=0;j<=(i-1);j++){
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
}
}
}
Interview problems
In CPP (Both using Loop and Using Recurrsion)
// void swap(int &i, int &j){
// int temp = i;
// i = j;
// j = temp;
// }
// __________using Loops__________
// void bubbleSort(vector<int>& arr, int n)
// {
// //write your code here
// for(int i=0; i<n; i++){
// for(int j=0; j<n-i-1; j++){
// if(arr[j] > arr[j+1]){
// swap(arr[j], arr[j+1]);
// }
// }
// }
// }
// __________using Recursion__________
void bubbleSort(vector<int>& arr, int n) {
if(n-1 <= 0) return;
for(int i =0; i<n-1; i++){
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
bubbleSort(arr, n-1);
}
Interview problems
Bubble Sort Algorithm using Java
public class Solution {
public static void bubbleSort(int[] arr, int n) {
//Your code goes here
int flag=0;
for(int i=n-2;i>=0;i--)
{
for(int j=0;j<=i;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
}
}
Interview problems
Recursive C++ solution
void bubbleSortRecursive(vector<int>& arr, int n, int i )
{
int temp=0;
if (i>n -2)
{
return;
}
if (arr[i]>= arr[i+1])
{
temp = arr[i];
arr[i]=arr[i+1];
arr[i+1] = temp;
}
bubbleSortRecursive(arr, n, i+1);
}
void bubbleSort(vector<int>& arr, int n)
{
if (n<0)
{
return;
}
bubbleSortRecursive(arr, n, 0);
bubbleSort(arr, n-1);
return;
}
Interview problems
Easy Python Solution (Usual approach & recursive approach both)
from typing import List
def bubbleSort(arr: List[int], n: int):
# Your code goes here.
#Without Recursion
# for i in range(n-1, 0, -1):
# for j in range(0, i):
# if arr[j] > arr[j+1]:
# arr[j], arr[j+1] = arr[j+1], arr[j]
#Using Recursion
if n == 1:
return
for j in range(n-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
bubbleSort(arr, n-1)
passInterview problems
Bubble Sort
void bubbleSort(vector<int>& arr, int n)
{
//write your code here
for(int i=0; i<n-1; i++){
for(int j=0; j<n-1-i; j++){
if(arr[j] > arr[j+1]){
swap(arr[j], arr[j+1]);
}
}
}
}
Interview problems
BUBBLE SORT
void bubbleSort(vector<int>& arr, int n)
{
int j;
for(int i=0;i<n-1;i++)
for(j=0; j<n-i-1; j++)
if(arr[j]>arr[j+1])
swap((arr[j]), arr[j+1]);
}
Interview problems
c++
void bubbleSort(vector<int>& arr, int n)
{
//write your code here
// sort(arr.begin(),arr.end());
for(int i=n-2;i>=0;i--){
for(int j=0;j<=i;j++){
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
}
}
}
Interview problems
one line c++ solution
void bubbleSort(vector<int>& arr, int n)
{
sort(arr.begin(),arr.end());
}