Skip to content

Commit

Permalink
Create Bubble_Sort.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
MAYANK25402 authored Feb 8, 2024
1 parent d1851a3 commit 3aa9351
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sorting Algorithms/Bubble_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}


if (swapped == false)
break;
}
}

void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
}


int main() // Main Function
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N); // Function Called
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}

0 comments on commit 3aa9351

Please sign in to comment.