0% found this document useful (0 votes)
12 views2 pages

Bubble Sort

Uploaded by

rohitk93639
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)
12 views2 pages

Bubble Sort

Uploaded by

rohitk93639
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/ 2

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int n) {

// Outer loop to traverse the entire array

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

// Inner loop to perform comparison and swapping

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

// If the element is greater than the next, swap them

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

// Swap the elements

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

void printArray(int arr[], int size) {

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

cout << arr[i] << " ";

cout << endl;

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original Array: ";

printArray(arr, n);

bubbleSort(arr, n);

cout << "Sorted Array: ";

printArray(arr, n);

return 0;

You might also like