From da1374702f61c731300bd4e06c9ca27ac4ec814e Mon Sep 17 00:00:00 2001 From: hao14293 Date: Mon, 19 Nov 2018 12:25:34 +0800 Subject: [PATCH] Create bubbleSort.cpp --- Data-Structure/Sort/sorts/bubbleSort.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Data-Structure/Sort/sorts/bubbleSort.cpp diff --git a/Data-Structure/Sort/sorts/bubbleSort.cpp b/Data-Structure/Sort/sorts/bubbleSort.cpp new file mode 100644 index 0000000..9e50291 --- /dev/null +++ b/Data-Structure/Sort/sorts/bubbleSort.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +int main(){ + int a[] = {6, 5, 2, 8, 4, 1, 3, 7}; + int len = sizeof(a) / sizeof(a[0]); + int m = len - 1, flag = 1; + while((m > 0) && (flag == 1)){ + flag = 0; + for(int i = 1; i <= m; i++){ + if(a[i - 1] > a[i]){ + flag = 1; + int temp = a[i - 1]; + a[i - 1] = a[i]; + a[i] = temp; + } + } + m--; + } + for(int i = 0; i < len; i++) + cout << a[i]; + return 0; +}