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