Logo

dev-resources.site

for different kinds of informations.

Bubble Sort

Published at
6/27/2023
Categories
bubblesort
sort
cpp
sorting
Author
ankittmeena
Categories
4 categories in total
bubblesort
open
sort
open
cpp
open
sorting
open
Author
11 person written this
ankittmeena
open
Bubble Sort

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,

  1. Traverse from left and compare adjacent elements and the higher one is placed at right side.
  2. In this way, the largest element is moved to the rightmost end at first.
  3. 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;
}
Enter fullscreen mode Exit fullscreen mode

Time Complexity: O(N2)
Auxiliary Space: O(1)

sorting Article's
30 articles in total
Favicon
Difference Between Merge Sort and Quick Sort
Favicon
Leetcode 75. Sort Colors
Favicon
Sorted Data Structures in Python
Favicon
Sorting Algorithms That Use Hash Tables
Favicon
C# Essentials: Operator Overloading and Custom Sorting Made Simple
Favicon
Recap the highlight of the sorting algorithms using JavaScript for beginners
Favicon
Merge Sort Demystified: A Beginner's Guide to Divide and Conquer Sorting
Favicon
Understanding Bubble Sort: Simple Sorting Method
Favicon
Introduction to Sorting Algorithms in JavaScript
Favicon
Understanding the SQL ORDER BY Clause
Favicon
Demystifying Sorting Algorithms: Making Order Out of Chaos
Favicon
Merge Intervals : A unique Graph-based approach
Favicon
Bubble Sort
Favicon
COMPARATOR vs COMPARABLEโ€Š-โ€ŠA Java Surprise You did inย School!
Favicon
Streamlining Data Management with Python's sorted() Function
Favicon
1 billion rows challenge in MySQL
Favicon
1 billion rows challenge in PostgreSQL and ClickHouse
Favicon
Sorting in Java โ€“ how to, and how not to do it anymore
Favicon
Reversing sort order in Rust
Favicon
Priority Queue: Creating order from chaos
Favicon
Mastering Array Sorting in PHP: usort & uasort ๐Ÿš€๐Ÿš€
Favicon
QuickSort - Time Analysis (Part2)
Favicon
Quicksort (Grokking Algorithms)
Favicon
Better Bogo Sort
Favicon
Sorting Array of Objects in Javascript
Favicon
Bubble Sort
Favicon
Understanding insertion sort algorithm
Favicon
Sorting Visualizer [ A web app to visualize sorting algorithm ]
Favicon
How to sort complex objects with custom criteria in Python
Favicon
Iterative Sorting algorithms in Javascript

Featured ones: