From 609975e88d746a7547a1a149d2566b8085117b82 Mon Sep 17 00:00:00 2001 From: Pranay711 <71118731+Pranay711@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:14:52 +0530 Subject: [PATCH] Update bubble_sort.h Bubble sort using template and funtion @pierredavidbelanger @Samana @blackball @wycg1984 --- include/bubble_sort.h | 67 ++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/include/bubble_sort.h b/include/bubble_sort.h index ca194ced..e81151db 100644 --- a/include/bubble_sort.h +++ b/include/bubble_sort.h @@ -23,29 +23,50 @@ #ifndef _BUBBLE_SORT_H_ #define _BUBBLE_SORT_H_ -#include -#include - -namespace alg { - template - static void BubbleSort(T list[], int start, int end){ - int i; - bool swapped; - - assert(start < end); - - do { - swapped = false; - for(i = start+1; i <= end; i++) { - if(list[i-1] > list[i]) { - // swap them and remember something changed - swap(list[i-1], list[i]); - swapped = true; - } - } - } while(swapped); - } +#include +using namespace std; + +template void bubble(X *items,int count) +{ + X t; + + for(int a=1; a=a; b--) + if(items[b-1] > items[b]) { + t = items[b-1]; + items[b-1] = items[b]; + items[b] = t; + } } +int main() +{ + int iarray[7] = {7, 5, 4, 3, 9, 8, 6}; + double darray[5] = {4.3, 2.5, -0.9, 10.2, 3.0}; + + cout << "Here is unsorted integer array: "; + for(int i=0; i<7; i++) + cout << iarray[i] << ' '; + cout << endl; + + bubble(iarray, 7); + + cout << "Here is sorted integer array: "; + for(int i=0; i<7; i++) + cout << iarray[i] << ' '; + cout << endl; -#endif // _BUBBLE_SORT_H_ + cout << "Here is unsorted double array: "; + for(int i=0; i<5; i++) + cout << darray[i] << ' '; + cout << endl; + + bubble(darray, 5); + + cout << "Here is sorted double array: "; + for(int i=0; i<5; i++) + cout << darray[i] << ' '; + cout << endl; + + return 0; +}