From 65be25f92bf7c45860093b06bf2abe3ef3d508d2 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:50:31 -0300 Subject: [PATCH] add bubble sort --- src/bubble_sort.c | 33 +++++++++++++++++++++++++++++++++ src/include/bubble_sort.h | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/bubble_sort.c create mode 100644 src/include/bubble_sort.h diff --git a/src/bubble_sort.c b/src/bubble_sort.c new file mode 100644 index 0000000..5b10ec0 --- /dev/null +++ b/src/bubble_sort.c @@ -0,0 +1,33 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +void bubble_sort(int *array, int n) { + int i; + int j = n; + int temp; + int flag = 1; + while (flag) { + flag = 0; + for (i = 1; i < j; i++) { + if (array[i] < array[i - 1]) { + temp = array[i]; + array[i] = array[i - 1]; + array[i - 1] = temp; + flag = 1; + } + } + j--; + } +} diff --git a/src/include/bubble_sort.h b/src/include/bubble_sort.h new file mode 100644 index 0000000..fc31615 --- /dev/null +++ b/src/include/bubble_sort.h @@ -0,0 +1,19 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SORT_BUBBLE + #define _SORT_BUBBLE + void bubble_sort(int *array, int n); +#endif