From b0e3807c44b20008fa2a4be52bed69094bb98022 Mon Sep 17 00:00:00 2001 From: omkarkurund <72203658+omkarkurund@users.noreply.github.com> Date: Tue, 6 Oct 2020 15:43:09 +0530 Subject: [PATCH 1/2] Bubble_sort.cpp --- cpp/rasacharjee_bubbleSort.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/rasacharjee_bubbleSort.cpp b/cpp/rasacharjee_bubbleSort.cpp index f69dd62d..6bdadfd2 100644 --- a/cpp/rasacharjee_bubbleSort.cpp +++ b/cpp/rasacharjee_bubbleSort.cpp @@ -20,12 +20,12 @@ void bubbleSort(ll arr[], ll size) int main() { - ll arr[5] = {32, 8, 2, 5, 7}; - bubbleSort(arr, 5); + ll arr[3] = {32, 8, 2}; + bubbleSort(arr, 3); cout << "the array after bubble sort is\n"; - for (ll i = 0; i < 5; i++) + for (ll i = 0; i < 3; i++) { cout << arr[i] << " "; } return 0; -} \ No newline at end of file +} From ada69afa74bcea3b0108e64bd9ae1ce3de61d65c Mon Sep 17 00:00:00 2001 From: omkarkurund <72203658+omkarkurund@users.noreply.github.com> Date: Sat, 12 Jun 2021 13:28:49 +0530 Subject: [PATCH 2/2] bubble_sort.cpp --- cpp/rasacharjee_bubbleSort.cpp | 66 +++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/cpp/rasacharjee_bubbleSort.cpp b/cpp/rasacharjee_bubbleSort.cpp index 6bdadfd2..6e13d570 100644 --- a/cpp/rasacharjee_bubbleSort.cpp +++ b/cpp/rasacharjee_bubbleSort.cpp @@ -1,31 +1,41 @@ -#include -#define ll long long int +#include using namespace std; -// Bubble Sort -void bubbleSort(ll arr[], ll size) -{ - for (ll i = 0; i < size; i++) - { - for (ll j = i; j < size; j++) - { - if (arr[i] > arr[j]) - { - ll temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - } - } +void swapping(int &a, int &b) { //swap the content of a and b + int temp; + temp = a; + a = b; + b = temp; } - -int main() -{ - ll arr[3] = {32, 8, 2}; - bubbleSort(arr, 3); - cout << "the array after bubble sort is\n"; - for (ll i = 0; i < 3; i++) - { - cout << arr[i] << " "; - } - return 0; +void display(int *array, int size) { + for(int i = 0; i array[j+1]) { //when the current item is bigger than next + swapping(array[j], array[j+1]); + swaps = 1; //set swap flag + } + } + if(!swaps) + break; // No swap in this pass, so array is sorted + } +} +int main() { + int n; + cout << "Enter the number of elements: "; + cin >> n; + int arr[n]; //create an array with given number of elements + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Array before Sorting: "; + display(arr, n); + bubbleSort(arr, n); + cout << "Array after Sorting: "; + display(arr, n); }