From 4bab2bfa109e38775146c1b3c53aa8e589f04072 Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Sat, 11 Mar 2017 22:43:13 -0800 Subject: [PATCH] Bubble sort --- LICENSE | 21 +++++++++++ README.md | 3 ++ c/bubble_sort.c | 81 +++++++++++++++++++++++++++++++++++++++++++ csharp/bubble_sort.cs | 62 +++++++++++++++++++++++++++++++++ go/bubble_sort.go | 59 +++++++++++++++++++++++++++++++ python/bubble_sort.py | 36 +++++++++++++++++++ ruby/bubble_sort.rb | 34 ++++++++++++++++++ 7 files changed, 296 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 c/bubble_sort.c create mode 100644 csharp/bubble_sort.cs create mode 100644 go/bubble_sort.go create mode 100644 python/bubble_sort.py create mode 100644 ruby/bubble_sort.rb diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..899186c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017, Kevin Cotugno + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8534cc7 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Ratio + +A collection of algorithms in a multitude of languages. diff --git a/c/bubble_sort.c b/c/bubble_sort.c new file mode 100644 index 0000000..3e671a4 --- /dev/null +++ b/c/bubble_sort.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017 Kevin Cotugno + * All rights reserved + * + * Distributed under the terms of the MIT software license. See the + * accompanying LICENSE file or http://www.opensource.org/licenses/MIT. + */ + +#include +#include +#include + +#define BUFFER_SIZE 256 + +int* bubble_sort(int* sorted, const int* values, const int count) +{ + int temp[count]; + int holder; + + memcpy(temp, values, sizeof(int) * count); + + for (int i = 0; i < count; i++) { + for (int j = 0; j < (count - i - 1); j++) { + if (temp[j] > temp[j + 1]) { + holder = temp[j]; + temp[j] = temp[j + 1]; + temp[j + 1] = holder; + } + } + } + + memcpy(sorted, temp, sizeof(int) * count); + return sorted; +} + +char* array_to_str(char* buffer, const int* array, int buffer_sz, int array_sz) +{ + char temp[BUFFER_SIZE]; + char result[BUFFER_SIZE]; + memset(result, '\0', sizeof(char) * BUFFER_SIZE); + + for (int i = 0; i < array_sz; i++) { + snprintf(temp, BUFFER_SIZE, "%d", array[i]); + strncat(result, temp, BUFFER_SIZE); + + if (i + 1 != array_sz) + strncat(result, ", ", 2); + } + + strncpy(buffer, result, buffer_sz - 1); + + return buffer; +} + +int main(void) +{ + char buffer[BUFFER_SIZE]; + int num; + + printf("All elements must be integers\n"); + printf("How many elements? "); + fgets(buffer, BUFFER_SIZE, stdin); + + num = atoi(buffer); + + int elements[num]; + for(int i = 0; i < num; i++) { + printf("Enter element %d: ", i + 1); + fgets(buffer, BUFFER_SIZE, stdin); + elements[i] = atoi(buffer); + } + + + int sorted[num]; + bubble_sort(sorted, elements, num); + + printf("Sorted: [%s]\n", array_to_str(buffer, sorted, BUFFER_SIZE, num)); + printf("Unsorted: [%s]\n", array_to_str(buffer, elements, BUFFER_SIZE, num)); + + return 0; +} diff --git a/csharp/bubble_sort.cs b/csharp/bubble_sort.cs new file mode 100644 index 0000000..b0f2270 --- /dev/null +++ b/csharp/bubble_sort.cs @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2017 Kevin Cotugno + * All rights reserved + * + * Distributed under the terms of the MIT software license. See the + * accompanying LICENSE file or http://www.opensource.org/licenses/MIT. + */ + +using System; + +class Ratio +{ + public static void Main(string[] args) + { + int num; + int[] elements; + + Console.WriteLine("All elements must integers"); + Console.Write("How many elements? "); + + try { + num = int.Parse(Console.ReadLine()); + + elements = new int[num]; + for (int i = 0; i < num; i++) + { + Console.Write("Enter element {0}: ", i + 1); + elements[i] = int.Parse(Console.ReadLine()); + } + + Console.WriteLine("Sorted: [{0}]", string.Join(", ", BubbleSort(elements))); + Console.WriteLine("Unsorted: [{0}]", string.Join(", ", elements)); + } + catch { + Environment.Exit(1); + } + + } + + private static int[] BubbleSort(int[] unsorted) + { + var sorted = new int[unsorted.Length]; + unsorted.CopyTo(sorted, 0); + var count = sorted.Length; + int t; + + for (int i = 0; i < count; i++) + { + for (int j = 0; j < (count - i - 1); j++) + { + if (sorted[j] > sorted[j + 1]) + { + t = sorted[j]; + sorted[j] = sorted[j + 1]; + sorted[j + 1] = t; + } + } + } + + return sorted; + } +} diff --git a/go/bubble_sort.go b/go/bubble_sort.go new file mode 100644 index 0000000..1b0049d --- /dev/null +++ b/go/bubble_sort.go @@ -0,0 +1,59 @@ +// Copyright (C) 2017 Kevin Cotugno +// All rights reserved +// +// Distributed under the terms of the MIT software license. See the +// accompanying LICENSE file or http://www.opensource.org/licenses/MIT. + +package main + +import ( + "fmt" + "strconv" +) + +func copy(to_copy []int) []int { + copy := make([]int, len(to_copy)) + + for i, v := range to_copy { + copy[i] = v + } + + return copy +} + +func BubbleSort(unsorted []int) []int { + sorted := copy(unsorted) + count := len(sorted) + + for i := 0; i < count; i++ { + for j := 0; j < (count - i - 1); j++ { + if sorted[j] > sorted[j + 1] { + t := sorted[j] + sorted[j] = sorted[j + 1] + sorted[j + 1] = t + } + } + } + + return sorted +} + +func main() { + var input string + fmt.Printf("All elements must be integers\n") + + fmt.Printf("How many elements? ") + fmt.Scanln(&input) + + num, _ := strconv.Atoi(input) + + var elements = make([]int, num) + for i := range elements { + fmt.Printf("Enter element %v: ", i + 1) + fmt.Scanln(&input) + elements[i], _ = strconv.Atoi(input) + } + + fmt.Printf("Sorted: %v\n", BubbleSort(elements)) + fmt.Printf("Unsorted: %v\n", elements) +} diff --git a/python/bubble_sort.py b/python/bubble_sort.py new file mode 100644 index 0000000..2b9e421 --- /dev/null +++ b/python/bubble_sort.py @@ -0,0 +1,36 @@ +# Copyright (C) 2017 Kevin Cotugno +# All rights reserved +# +# Distributed under the terms of the MIT software license. See the +# accompanying LICENSE file or http://www.opensource.org/licenses/MIT. + +import copy + +def bubble_sort(unsorted): + count = len(unsorted) + sort = copy.deepcopy(unsorted) + + for i in range(count): + for j in range(count - i - 1): + if sort[j] > sort[j + 1]: + t = sort[j] + sort[j] = sort[j + 1] + sort[j + 1] = t + + return sort + +print("All elements must be integers") +num = input("How many elements? ") + +try: + num = int(num) + + elements = [] + for i in range(num): + elements.append(int(input("Enter element {0}: ".format(i + 1)))) + +except: + exit(1) + +print("Sorted: {0}".format(bubble_sort(elements))) +print("Unsorted: {0}".format(elements)) diff --git a/ruby/bubble_sort.rb b/ruby/bubble_sort.rb new file mode 100644 index 0000000..897b2f6 --- /dev/null +++ b/ruby/bubble_sort.rb @@ -0,0 +1,34 @@ +# Copyright (C) 2017 Kevin Cotugno +# All rights reserved +# +# Distributed under the terms of the MIT software license. See the +# accompanying LICENSE file or http://www.opensource.org/licenses/MIT. + +def bubble_sort(unsorted) + sorted = unsorted.clone + count = sorted.count + + count.times do |i| + (count - i - 1).times do |j| + if sorted[j] > sorted[j+1] + t = sorted[j] + sorted[j] = sorted[j+1] + sorted[j+1] = t + end + end + end + + sorted +end + +puts 'All elements must be integers' +print 'How many elements? ' +num = gets.to_i + +elements = Array.new(num) do |i| + print "Enter element #{i + 1}: " + gets.to_i +end + +print "Sorted: #{elements}\n" +print "Unsorted: #{bubble_sort(elements)}\n"