Bubble sort
This commit is contained in:
commit
4bab2bfa10
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -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.
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Ratio
|
||||||
|
|
||||||
|
A collection of algorithms in a multitude of languages.
|
81
c/bubble_sort.c
Normal file
81
c/bubble_sort.c
Normal file
@ -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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
62
csharp/bubble_sort.cs
Normal file
62
csharp/bubble_sort.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
59
go/bubble_sort.go
Normal file
59
go/bubble_sort.go
Normal file
@ -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)
|
||||||
|
}
|
36
python/bubble_sort.py
Normal file
36
python/bubble_sort.py
Normal file
@ -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))
|
34
ruby/bubble_sort.rb
Normal file
34
ruby/bubble_sort.rb
Normal file
@ -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"
|
Loading…
Reference in New Issue
Block a user