diff options
Merge branch 'main' of https://github.com/Angeld55/Introduction_to_programming_FMI
1 files changed, 39 insertions, 0 deletions
| -rw-r--r-- | Sem. 11/BubbleSort.cpp | 39 |
diff --git a/Sem. 11/BubbleSort.cpp b/Sem. 11/BubbleSort.cpp new file mode 100644 index 0000000..217bf32 --- /dev/null +++ b/Sem. 11/BubbleSort.cpp @@ -0,0 +1,39 @@ +#include <iostream> +using namespace std; + + +void swap(int& a, int& b) +{ + int temp = a; + a = b; + b = temp; +} + +void bubbleSort(int* arr, unsigned size) +{ + for (unsigned j = 0; j < size - 1; j++) + { + bool isSwapped = false; + + for (unsigned i = 0; i < size - 1 - j; i++) + { + if (arr[i] > arr[i + 1]) + { + swap(arr[i], arr[i + 1]); + isSwapped = true; + } + } + + if (!isSwapped) + return; + } +} + +int main() +{ + int arr[] = { 1,2,2,1, 1 }; + selectionSort(arr, 5); + + for (int i = 0; i < 5; i++) + cout << arr[i] << " "; +} |
