0% found this document useful (0 votes)
8 views

Bubble Sort

The document provides an implementation of the Bubble Sort algorithm in C++, detailing its functionality and structure. It includes a main function that prompts the user for input, sorts the array using Bubble Sort, and prints the sorted result. Additionally, it outlines the algorithm's steps and presents pseudocode for clarity.

Uploaded by

brotin2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Bubble Sort

The document provides an implementation of the Bubble Sort algorithm in C++, detailing its functionality and structure. It includes a main function that prompts the user for input, sorts the array using Bubble Sort, and prints the sorted result. Additionally, it outlines the algorithm's steps and presents pseudocode for clarity.

Uploaded by

brotin2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

BUBBLE SORT::::::

#include <iostream>

#include <vector>

// Function to implement Bubble Sort

void bubbleSort(std::vector<int> &arr) {

int n = arr.size();

for (int i = 0; i < n - 1; ++i) {

// Flag to check if any swaps occurred in this pass

bool swapped = false;

for (int j = 0; j < n - i - 1; ++j) {

if (arr[j] > arr[j + 1]) {

// Swap adjacent elements if they are in the wrong order

std::swap(arr[j], arr[j + 1]);

swapped = true;

// If no swaps occurred, the array is already sorted

if (!swapped) break;

int main() {

int n;

std::cout << "Enter the number of elements: ";

std::cin >> n;

std::vector<int> arr(n);

std::cout << "Enter the elements:\n";

for (int i = 0; i < n; ++i) {

std::cin >> arr[i];

// Perform Bubble Sort


bubbleSort(arr);

// Print the sorted array

std::cout << "Sorted array: ";

for (const int &num : arr) {

std::cout << num << " ";

std::cout << std::endl;

return 0;

Algorithm for Bubble Sort::

1. Initialize:

o Start with an array of n elements.

o We perform a series of passes through the array.

2. Traverse the Array:

o For each pass (up to n-1 passes):

 Compare adjacent pairs of elements (arr[j] and arr[j+1]).

 If the left element (arr[j]) is greater than the right element (arr[j+1]), swap them.

3. Continue Until No Swaps:

o After each pass, the largest element is "bubbled" to the end of the array.

o In subsequent passes, reduce the size of the array to consider only the unsorted portion.

4. Optimization (Optional):

o If during a pass no swaps are made, it indicates that the array is already sorted, and you can
terminate the loop early to save time.

PSEUDOCODE::::::

BUBBLE_SORT(arr):

n = size of arr

FOR i FROM 0 TO n-2 DO:

swapped = false

FOR j FROM 0 TO n-i-2 DO:

IF arr[j] > arr[j+1] THEN:

SWAP arr[j] AND arr[j+1]


swapped = true

// If no two elements were swapped, the array is already sorted

IF swapped == false THEN:

BREAK

You might also like