diff --git a/Sort/bubbleSort.cpp b/Sort/bubbleSort.cpp new file mode 100644 index 00000000..8bf86de1 --- /dev/null +++ b/Sort/bubbleSort.cpp @@ -0,0 +1,46 @@ +#include + +void printArray(int *array, int n) +{ + for (int i = 0; i < n; ++i) + std::cout << array[i] << std::endl; +} + +void bubbleSort(int *array, int n) +{ + bool swapped = true; + int j = 0; + int temp; + + while (swapped) + { + swapped = false; + j++; + for (int i = 0; i < n - j; ++i) + { + if (array[i] > array[i + 1]) + { + temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + swapped = true; + } + } + } +} + +int main() +{ + int array[] = {95, 45, 48, 98, 485, 65, 54, 478, 1, 2325}; + int n = sizeof(array)/sizeof(array[0]); + + std::cout << "Before Bubble Sort :" << std::endl; + printArray(array, n); + + bubbleSort(array, n); + + std::cout << "After Bubble Sort :" << std::endl; + printArray(array, n); + return (0); +} +@anmolch24