dev-resources.site
for different kinds of informations.
Bubble Sort
Published at
6/27/2023
Categories
bubblesort
sort
cpp
sorting
Author
ankittmeena
Main Article
Author
11 person written this
ankittmeena
open
Bubble Sort:
It is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.
In this algorithm,
- Traverse from left and compare adjacent elements and the higher one is placed at right side.
- In this way, the largest element is moved to the rightmost end at first.
- This process is then continued to find the second largest and place it and so on until the data is sorted.
C++ Code
#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 print(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
}
int main()
{
int arr[] = { 23, 45, 65, 23, 57, 88 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
print(arr, N);
return 0;
}
Time Complexity: O(N2)
Auxiliary Space: O(1)
sorting Article's
30 articles in total
Difference Between Merge Sort and Quick Sort
read article
Leetcode 75. Sort Colors
read article
Sorted Data Structures in Python
read article
Sorting Algorithms That Use Hash Tables
read article
C# Essentials: Operator Overloading and Custom Sorting Made Simple
read article
Recap the highlight of the sorting algorithms using JavaScript for beginners
read article
Merge Sort Demystified: A Beginner's Guide to Divide and Conquer Sorting
read article
Understanding Bubble Sort: Simple Sorting Method
read article
Introduction to Sorting Algorithms in JavaScript
read article
Understanding the SQL ORDER BY Clause
read article
Demystifying Sorting Algorithms: Making Order Out of Chaos
read article
Merge Intervals : A unique Graph-based approach
read article
Bubble Sort
read article
COMPARATOR vs COMPARABLEโ-โA Java Surprise You did inย School!
read article
Streamlining Data Management with Python's sorted() Function
read article
1 billion rows challenge in MySQL
read article
1 billion rows challenge in PostgreSQL and ClickHouse
read article
Sorting in Java โ how to, and how not to do it anymore
read article
Reversing sort order in Rust
read article
Priority Queue: Creating order from chaos
read article
Mastering Array Sorting in PHP: usort & uasort ๐๐
read article
QuickSort - Time Analysis (Part2)
read article
Quicksort (Grokking Algorithms)
read article
Better Bogo Sort
read article
Sorting Array of Objects in Javascript
read article
Bubble Sort
currently reading
Understanding insertion sort algorithm
read article
Sorting Visualizer [ A web app to visualize sorting algorithm ]
read article
How to sort complex objects with custom criteria in Python
read article
Iterative Sorting algorithms in Javascript
read article
Featured ones: