Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions cpp/rasacharjee_bubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
#include <bits/stdc++.h>
#define ll long long int
#include<iostream>
using namespace std;
// Bubble Sort
void bubbleSort(ll arr[], ll size)
{
for (ll i = 0; i < size; i++)
{
for (ll j = i; j < size; j++)
{
if (arr[i] > arr[j])
{
ll temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
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; //flag to detect any swap is there or not
for(int j = 0; j<size-i-1; j++) {
if(array[j] > array[j+1]) { //when the current item is bigger than next
swapping(array[j], array[j+1]);
swaps = 1; //set swap flag
}
}
if(!swaps)
break; // No swap in this pass, so array is sorted
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
bubbleSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}

int main()
{
ll arr[5] = {32, 8, 2, 5, 7};
bubbleSort(arr, 5);
cout << "the array after bubble sort is\n";
for (ll i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
return 0;
}