Don't copy

This commit is contained in:
Kevin Cotugno 2017-03-12 09:42:14 -07:00
parent 4bab2bfa10
commit 3abf80e027
5 changed files with 35 additions and 60 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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))
}

View File

@ -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)))

View File

@ -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"