diff --git a/sorting-algorithms/bubble_sort.cpp b/sorting-algorithms/bubble_sort.cpp new file mode 100644 index 0000000..c6faa7a --- /dev/null +++ b/sorting-algorithms/bubble_sort.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +/* + * BubbleSort: + * sorting algorithm that works by repeatedly swapping the adjacent elements + * if they are in wrong order + * + * Average/Worst Case Time Complexity: O(n*n) + * Best Case Time Complexity: O(n) + */ + +// change location of two arrays +void swap(int &a, int &b) { + int tmp; + tmp = a; + a = b; + b = tmp; +} + +// sort array in ascending order +void BubbleSort(int arr[], int size) { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size-i-1; j++) { + if (arr[j] > arr[j + 1]) + swap(arr[j], arr[j + 1]); + } + } +} + +int main(){ + int size; + int* array; + cout << "Size: "; + cin >> size; + array = new int[size]; + for(int i=0;i> array[i]; + } + BubbleSort(array, size); + for(int j=0;j