-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sorting.cpp
More file actions
54 lines (50 loc) · 1.74 KB
/
bubble-sorting.cpp
File metadata and controls
54 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @file BubbleSort.cpp
* @brief Template implementation of the Bubble Sort algorithm.
* * Bubble sort works by repeatedly stepping through the list, comparing
* adjacent elements and swapping them if they are in the wrong order.
*/
#include <iostream>
using namespace std;
/**
* @brief Sorts a fixed-size array using the optimized Bubble Sort algorithm.
* * This implementation utilizes a 'swapped' flag to optimize the best-case
* scenario. If no elements are swapped during a pass, the array is already
* sorted, and the function terminates early.
* * @tparam T The data type of the array elements (must support comparison operators).
* @tparam n The size of the array, deduced by the compiler via reference.
* @param arr A reference to the array to be sorted.
*/
template <typename T, size_t n>
void bubbleSort(T (&arr)[n])
{
bool swapped;
// Outer loop for number of passes
for (size_t i = 0; i < n - 1; i++)
{
swapped = false;
// Inner loop for comparing adjacent elements
for (size_t j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[arr[j + 1]]);
swapped = true; // Mark that a swap occurred
}
}
// Optimization: If no two elements were swapped, array is sorted
if (!swapped)
{
break;
}
}
}
/**
* Technical Implementation Details:
* 1. Pass-by-reference 'T (&arr)[n]' allows the compiler to handle size deduction.
* 2. Time Complexity:
* - Best Case: O(n) (when array is already sorted).
* - Average/Worst Case: O(n^2).
* 3. Space Complexity: O(1) as it is an in-place sorting algorithm.
* 4. Stability: Bubble Sort is a stable sorting algorithm.
*/