Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / BubbleSort.cpp
diff options
context:
space:
mode:
Commit4fb718f5fc6088641ecad2aeccd321b77b8bd356 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Files7f0abd8e37eed78ccc0b45a65de80450d1c59eef /Sem. 11/BubbleSort.cpp
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar.gz
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.zip
Parent158370669b24142a71ce7778b46f5a054cafed8a (diff)
Parentb3e29bba94da067984c841d7904887a4bd5995ea (diff)
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.cpp39
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] << " ";
+}