Don't copy
This commit is contained in:
parent
4bab2bfa10
commit
3abf80e027
@ -12,36 +12,32 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 256
|
#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 t;
|
||||||
int holder;
|
|
||||||
|
|
||||||
memcpy(temp, values, sizeof(int) * count);
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
for (int j = 0; j < (count - i - 1); j++) {
|
for (int j = 0; j < (count - i - 1); j++) {
|
||||||
if (temp[j] > temp[j + 1]) {
|
if (unsorted[j] > unsorted[j + 1]) {
|
||||||
holder = temp[j];
|
t = unsorted[j];
|
||||||
temp[j] = temp[j + 1];
|
unsorted[j] = unsorted[j + 1];
|
||||||
temp[j + 1] = holder;
|
unsorted[j + 1] = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(sorted, temp, sizeof(int) * count);
|
return unsorted;
|
||||||
return sorted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* array_to_str(char* buffer, const int* array, int buffer_sz, int array_sz)
|
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];
|
char result[BUFFER_SIZE];
|
||||||
memset(result, '\0', sizeof(char) * BUFFER_SIZE);
|
memset(result, '\0', sizeof(char) * BUFFER_SIZE);
|
||||||
|
|
||||||
for (int i = 0; i < array_sz; i++) {
|
for (int i = 0; i < array_sz; i++) {
|
||||||
snprintf(temp, BUFFER_SIZE, "%d", array[i]);
|
snprintf(unsorted, BUFFER_SIZE, "%d", array[i]);
|
||||||
strncat(result, temp, BUFFER_SIZE);
|
strncat(result, unsorted, BUFFER_SIZE);
|
||||||
|
|
||||||
if (i + 1 != array_sz)
|
if (i + 1 != array_sz)
|
||||||
strncat(result, ", ", 2);
|
strncat(result, ", ", 2);
|
||||||
@ -57,7 +53,6 @@ int main(void)
|
|||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
int num;
|
int num;
|
||||||
|
|
||||||
printf("All elements must be integers\n");
|
|
||||||
printf("How many elements? ");
|
printf("How many elements? ");
|
||||||
fgets(buffer, BUFFER_SIZE, stdin);
|
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));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ class Ratio
|
|||||||
int num;
|
int num;
|
||||||
int[] elements;
|
int[] elements;
|
||||||
|
|
||||||
Console.WriteLine("All elements must integers");
|
|
||||||
Console.Write("How many elements? ");
|
Console.Write("How many elements? ");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -28,8 +27,8 @@ class Ratio
|
|||||||
elements[i] = int.Parse(Console.ReadLine());
|
elements[i] = int.Parse(Console.ReadLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Sorted: [{0}]", string.Join(", ", BubbleSort(elements)));
|
|
||||||
Console.WriteLine("Unsorted: [{0}]", string.Join(", ", elements));
|
Console.WriteLine("Unsorted: [{0}]", string.Join(", ", elements));
|
||||||
|
Console.WriteLine("Sorted: [{0}]", string.Join(", ", BubbleSort(elements)));
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Environment.Exit(1);
|
Environment.Exit(1);
|
||||||
@ -39,24 +38,22 @@ class Ratio
|
|||||||
|
|
||||||
private static int[] BubbleSort(int[] unsorted)
|
private static int[] BubbleSort(int[] unsorted)
|
||||||
{
|
{
|
||||||
var sorted = new int[unsorted.Length];
|
var count = unsorted.Length;
|
||||||
unsorted.CopyTo(sorted, 0);
|
|
||||||
var count = sorted.Length;
|
|
||||||
int t;
|
int t;
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < (count - i - 1); j++)
|
for (int j = 0; j < (count - i - 1); j++)
|
||||||
{
|
{
|
||||||
if (sorted[j] > sorted[j + 1])
|
if (unsorted[j] > unsorted[j + 1])
|
||||||
{
|
{
|
||||||
t = sorted[j];
|
t = unsorted[j];
|
||||||
sorted[j] = sorted[j + 1];
|
unsorted[j] = unsorted[j + 1];
|
||||||
sorted[j + 1] = t;
|
unsorted[j + 1] = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sorted;
|
return unsorted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,36 +11,24 @@ import (
|
|||||||
"strconv"
|
"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 {
|
func BubbleSort(unsorted []int) []int {
|
||||||
sorted := copy(unsorted)
|
count := len(unsorted)
|
||||||
count := len(sorted)
|
|
||||||
|
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
for j := 0; j < (count - i - 1); j++ {
|
for j := 0; j < (count - i - 1); j++ {
|
||||||
if sorted[j] > sorted[j + 1] {
|
if unsorted[j] > unsorted[j + 1] {
|
||||||
t := sorted[j]
|
t := unsorted[j]
|
||||||
sorted[j] = sorted[j + 1]
|
unsorted[j] = unsorted[j + 1]
|
||||||
sorted[j + 1] = t
|
unsorted[j + 1] = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sorted
|
return unsorted
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var input string
|
var input string
|
||||||
fmt.Printf("All elements must be integers\n")
|
|
||||||
|
|
||||||
fmt.Printf("How many elements? ")
|
fmt.Printf("How many elements? ")
|
||||||
fmt.Scanln(&input)
|
fmt.Scanln(&input)
|
||||||
@ -54,6 +42,6 @@ func main() {
|
|||||||
elements[i], _ = strconv.Atoi(input)
|
elements[i], _ = strconv.Atoi(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Sorted: %v\n", BubbleSort(elements))
|
|
||||||
fmt.Printf("Unsorted: %v\n", elements)
|
fmt.Printf("Unsorted: %v\n", elements)
|
||||||
|
fmt.Printf("Sorted: %v\n", BubbleSort(elements))
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ def bubble_sort(unsorted):
|
|||||||
|
|
||||||
return sort
|
return sort
|
||||||
|
|
||||||
print("All elements must be integers")
|
|
||||||
num = input("How many elements? ")
|
num = input("How many elements? ")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -32,5 +31,5 @@ try:
|
|||||||
except:
|
except:
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
print("Sorted: {0}".format(bubble_sort(elements)))
|
|
||||||
print("Unsorted: {0}".format(elements))
|
print("Unsorted: {0}".format(elements))
|
||||||
|
print("Sorted: {0}".format(bubble_sort(elements)))
|
||||||
|
@ -5,23 +5,21 @@
|
|||||||
# accompanying LICENSE file or http://www.opensource.org/licenses/MIT.
|
# accompanying LICENSE file or http://www.opensource.org/licenses/MIT.
|
||||||
|
|
||||||
def bubble_sort(unsorted)
|
def bubble_sort(unsorted)
|
||||||
sorted = unsorted.clone
|
count = unsorted.count
|
||||||
count = sorted.count
|
|
||||||
|
|
||||||
count.times do |i|
|
count.times do |i|
|
||||||
(count - i - 1).times do |j|
|
(count - i - 1).times do |j|
|
||||||
if sorted[j] > sorted[j+1]
|
if unsorted[j] > unsorted[j+1]
|
||||||
t = sorted[j]
|
t = unsorted[j]
|
||||||
sorted[j] = sorted[j+1]
|
unsorted[j] = unsorted[j+1]
|
||||||
sorted[j+1] = t
|
unsorted[j+1] = t
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
sorted
|
unsorted
|
||||||
end
|
end
|
||||||
|
|
||||||
puts 'All elements must be integers'
|
|
||||||
print 'How many elements? '
|
print 'How many elements? '
|
||||||
num = gets.to_i
|
num = gets.to_i
|
||||||
|
|
||||||
@ -30,5 +28,5 @@ elements = Array.new(num) do |i|
|
|||||||
gets.to_i
|
gets.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
print "Sorted: #{elements}\n"
|
print "Unsorted: #{elements}\n"
|
||||||
print "Unsorted: #{bubble_sort(elements)}\n"
|
print "Sorted: #{bubble_sort(elements)}\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user