From a98b1c48db8ea688fed202c412e34b77182bc460 Mon Sep 17 00:00:00 2001 From: DarksideRoy Date: Mon, 13 Apr 2020 22:55:03 +0800 Subject: [PATCH 1/3] 655249 bubble sort --- 655249.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 655249.cpp diff --git a/655249.cpp b/655249.cpp new file mode 100644 index 0000000..1302d1a --- /dev/null +++ b/655249.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main() { + int n; + cout << "How many numbers?"; + cin >> n; + + int a[n]; + + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (a[i] > a[j]) { + int vemp; + vemp = a[i]; + a[i] = a[j]; + a[j] = vemp; + } + } + } + + cout << "\nAfter bubble sort...\n"; + + for (int i = 0; i < n; i++) { + cout << a[i] << " "; + } +} \ No newline at end of file From 0f0f74538583314a2537505be488fa8b7d00e656 Mon Sep 17 00:00:00 2001 From: DarksideRoy Date: Mon, 13 Apr 2020 22:56:48 +0800 Subject: [PATCH 2/3] 655249 --- 655249.cpp => 655249/655249.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 655249.cpp => 655249/655249.cpp (100%) diff --git a/655249.cpp b/655249/655249.cpp similarity index 100% rename from 655249.cpp rename to 655249/655249.cpp From 81075901ab31f5f27efb7aac6a5671e2863ed417 Mon Sep 17 00:00:00 2001 From: DarksideRoy <54939541+DarksideRoy@users.noreply.github.com> Date: Thu, 7 May 2020 19:38:54 +0800 Subject: [PATCH 3/3] Update 655249.cpp --- 655249/655249.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/655249/655249.cpp b/655249/655249.cpp index 1302d1a..246db17 100644 --- a/655249/655249.cpp +++ b/655249/655249.cpp @@ -1,6 +1,8 @@ #include using namespace std; +void swap(int *a, int *b); + int main() { int n; cout << "How many numbers?"; @@ -14,12 +16,9 @@ int main() { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { - if (a[i] > a[j]) { - int vemp; - vemp = a[i]; - a[i] = a[j]; - a[j] = vemp; - } + if (a[i] > a[j]) { + swap(&a[i], &a[j]); + } } } @@ -28,4 +27,10 @@ int main() { for (int i = 0; i < n; i++) { cout << a[i] << " "; } -} \ No newline at end of file +} + +void swap(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; +}