0% found this document useful (0 votes)
11 views3 pages

BubbleSort Algorithm

Bubble Sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until the list is sorted. The document includes a C++ implementation of the Bubble Sort algorithm along with a function to print the sorted array. Additionally, there are sections labeled 'Activity no. 1' and 'Activity no. 2' with numbers, but their context is unclear.
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)
11 views3 pages

BubbleSort Algorithm

Bubble Sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until the list is sorted. The document includes a C++ implementation of the Bubble Sort algorithm along with a function to print the sorted array. Additionally, there are sections labeled 'Activity no. 1' and 'Activity no. 2' with numbers, but their context is unclear.
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

Name: Total Score:

BubbleSort Algorithm

Bubble Sort is a sorting algorithm that compares two adjacent elements in a list and swaps them if they are in the wrong order. This
process is repeated until the entire list is sorted. The largest elements "bubble up" to their correct positions with each pass.

Implementation Score:
Below is the implementation of the above approach

// C++ program for implementation void printArray(int arr[], int size)


// of Bubble sort {
#include <bits/stdc++.h> int i;
using namespace std; for (i = 0; i < size; i++)
cout << arr[i] << " ";
// A function to implement bubble sort cout << endl;
void bubbleSort(int arr[], int n) }
{
int i, j; // Driver code
for (i = 0; i < n - 1; i++) int main()
{
// Last i elements are already int arr[] = { 5, 1, 4, 2, 8};
// in place int N = sizeof(arr) / sizeof(arr[0]);
for (j = 0; j < n - i - 1; j++) bubbleSort(arr, N);
if (arr[j] > arr[j + 1]) cout << "Sorted array: \n";
swap(arr[j], arr[j + 1]); printArray(arr, N);
} return 0;
}
// Function to print an array

Let’s try!

Name: Score:

Activity no. 1

3 2 1 5 6 4
Activity no. 2

7 -4 3 -2 1 8 2

You might also like