Cycle 01
Lab Cycle 1 — Arrays & Searching
Bubble Sort
Problem Statement
Write a program to sort an array using bubble sort.
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 34, 25, 12, 22};
int n = 5;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}
}
for (int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}
Question 5 of 5