From 1c0eb3871d885c8d1155065ec2d8bc1e87b95035 Mon Sep 17 00:00:00 2001 From: GitanshKapoor <72307552+GitanshKapoor@users.noreply.github.com> Date: Sat, 3 Oct 2020 19:48:44 +0530 Subject: [PATCH] Update bubbleSort.h --- bubbleSort.h | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/bubbleSort.h b/bubbleSort.h index bce488d..04f78ed 100644 --- a/bubbleSort.h +++ b/bubbleSort.h @@ -1,26 +1,19 @@ #include - +#include using namespace std; // BUBBLE SORT //------------------------------------------------------- -void bubbleSort(vector &v, int n) { - bool swapped = true; - int i = 0; - - while (i < n - 1 && swapped) { // keep going while we swap in the unordered part - swapped = false; - - for (int j = n - 1; j > i; j--) { // unordered part - - if (v[j] < v[j - 1]) { - swap(v[j], v[j - 1]); - swapped = true; - } - } - i++; - } +void bubbleSort(vector &a, int n) { + int i, j; + for(i = 0; i < n-1; ++i) { + for(j = 0; j < n-i-1; ++j) { + if(a[j+1] < a[j]) { + swap(a[j],a[j+1]); + } + } + } }