From 3abf80e0276f012d684364357576ad4ee0ed264a Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Sun, 12 Mar 2017 09:42:14 -0700 Subject: [PATCH] Don't copy --- c/bubble_sort.c | 31 ++++++++++++------------------- csharp/bubble_sort.cs | 17 +++++++---------- go/bubble_sort.go | 26 +++++++------------------- python/bubble_sort.py | 3 +-- ruby/bubble_sort.rb | 18 ++++++++---------- 5 files changed, 35 insertions(+), 60 deletions(-) diff --git a/c/bubble_sort.c b/c/bubble_sort.c index 3e671a4..fe27501 100644 --- a/c/bubble_sort.c +++ b/c/bubble_sort.c @@ -12,36 +12,32 @@ #define BUFFER_SIZE 256 -int* bubble_sort(int* sorted, const int* values, const int count) +int* bubble_sort(int* unsorted, const int count) { - int temp[count]; - int holder; - - memcpy(temp, values, sizeof(int) * count); + int t; 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; + if (unsorted[j] > unsorted[j + 1]) { + t = unsorted[j]; + unsorted[j] = unsorted[j + 1]; + unsorted[j + 1] = t; } } } - memcpy(sorted, temp, sizeof(int) * count); - return sorted; + return unsorted; } char* array_to_str(char* buffer, const int* array, int buffer_sz, int array_sz) { - char temp[BUFFER_SIZE]; + char unsorted[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); + snprintf(unsorted, BUFFER_SIZE, "%d", array[i]); + strncat(result, unsorted, BUFFER_SIZE); if (i + 1 != array_sz) strncat(result, ", ", 2); @@ -57,7 +53,6 @@ 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); @@ -71,11 +66,9 @@ int main(void) } - 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)); + bubble_sort(elements, num); + printf("Sorted: [%s]\n", array_to_str(buffer, elements, BUFFER_SIZE, num)); return 0; } diff --git a/csharp/bubble_sort.cs b/csharp/bubble_sort.cs index b0f2270..8260129 100644 --- a/csharp/bubble_sort.cs +++ b/csharp/bubble_sort.cs @@ -15,7 +15,6 @@ class Ratio int num; int[] elements; - Console.WriteLine("All elements must integers"); Console.Write("How many elements? "); try { @@ -28,8 +27,8 @@ class Ratio elements[i] = int.Parse(Console.ReadLine()); } - Console.WriteLine("Sorted: [{0}]", string.Join(", ", BubbleSort(elements))); Console.WriteLine("Unsorted: [{0}]", string.Join(", ", elements)); + Console.WriteLine("Sorted: [{0}]", string.Join(", ", BubbleSort(elements))); } catch { Environment.Exit(1); @@ -39,24 +38,22 @@ class Ratio private static int[] BubbleSort(int[] unsorted) { - var sorted = new int[unsorted.Length]; - unsorted.CopyTo(sorted, 0); - var count = sorted.Length; + var count = unsorted.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]) + if (unsorted[j] > unsorted[j + 1]) { - t = sorted[j]; - sorted[j] = sorted[j + 1]; - sorted[j + 1] = t; + t = unsorted[j]; + unsorted[j] = unsorted[j + 1]; + unsorted[j + 1] = t; } } } - return sorted; + return unsorted; } } diff --git a/go/bubble_sort.go b/go/bubble_sort.go index 1b0049d..0a6db0e 100644 --- a/go/bubble_sort.go +++ b/go/bubble_sort.go @@ -11,36 +11,24 @@ import ( "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) + count := len(unsorted) 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 + if unsorted[j] > unsorted[j + 1] { + t := unsorted[j] + unsorted[j] = unsorted[j + 1] + unsorted[j + 1] = t } } } - return sorted + return unsorted } func main() { var input string - fmt.Printf("All elements must be integers\n") fmt.Printf("How many elements? ") fmt.Scanln(&input) @@ -54,6 +42,6 @@ func main() { elements[i], _ = strconv.Atoi(input) } - fmt.Printf("Sorted: %v\n", BubbleSort(elements)) fmt.Printf("Unsorted: %v\n", elements) + fmt.Printf("Sorted: %v\n", BubbleSort(elements)) } diff --git a/python/bubble_sort.py b/python/bubble_sort.py index 2b9e421..2aa2cbe 100644 --- a/python/bubble_sort.py +++ b/python/bubble_sort.py @@ -19,7 +19,6 @@ def bubble_sort(unsorted): return sort -print("All elements must be integers") num = input("How many elements? ") try: @@ -32,5 +31,5 @@ try: except: exit(1) -print("Sorted: {0}".format(bubble_sort(elements))) print("Unsorted: {0}".format(elements)) +print("Sorted: {0}".format(bubble_sort(elements))) diff --git a/ruby/bubble_sort.rb b/ruby/bubble_sort.rb index 897b2f6..33cc0a0 100644 --- a/ruby/bubble_sort.rb +++ b/ruby/bubble_sort.rb @@ -5,23 +5,21 @@ # accompanying LICENSE file or http://www.opensource.org/licenses/MIT. def bubble_sort(unsorted) - sorted = unsorted.clone - count = sorted.count + count = unsorted.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 + if unsorted[j] > unsorted[j+1] + t = unsorted[j] + unsorted[j] = unsorted[j+1] + unsorted[j+1] = t end end end - sorted + unsorted end -puts 'All elements must be integers' print 'How many elements? ' num = gets.to_i @@ -30,5 +28,5 @@ elements = Array.new(num) do |i| gets.to_i end -print "Sorted: #{elements}\n" -print "Unsorted: #{bubble_sort(elements)}\n" +print "Unsorted: #{elements}\n" +print "Sorted: #{bubble_sort(elements)}\n"