Skip to content

Instantly share code, notes, and snippets.

@vgmoose vgmoose/Java_BS.java
Last active Feb 25, 2016

Embed
What would you like to do?
Bubble sort in multiple languages
#include <stdio.h>
#define SIZE 8
int main()
{
// Declare Array
int A[SIZE] = {6,5,3,1,8,7,2,4};
// Set length
int N = SIZE;
// as long as there are things left to compare
while (N > 0)
{
// put 0 into i
int i = 0;
// while i is less than N
while (i < N-1)
{
// if left > right
if (A[i] > A[i+1])
{
// swap them
int temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
// increment i
i += 1;
}
// decrease total things to compare
N -= 1;
}
// print the array
for (int c=0; c<SIZE; c++)
printf("%d, ", A[c]);
printf("\n");
}
import java.util.Arrays;
public class Java_BS
{
public static void main(String[] args)
{
// Declare Array
int[] A = {6,5,3,1,8,7,2,4};
// Set length
int N = A.length;
// as long as there are things left to compare
while (N > 0)
{
// put 0 into i
int i = 0;
// while i is less than N
while (i < N-1)
{
// if left > right
if (A[i] > A[i+1])
{
// swap them
int temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
// increment i
i += 1;
}
// decrease total things to compare
N -= 1;
}
// print the array
System.out.println(Arrays.toString(A));
}
}
// Declare Array
var A = [6,5,3,1,8,7,2,4];
// Set length
var N = A.length;
// as long as there are things left to compare
while (N > 0)
{
// put 0 into i
var i = 0;
// while i is less than N
while (i < N-1)
{
// if left > right
if (A[i] > A[i+1])
{
// swap them
var temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
// increment i
i += 1;
}
// decrease total things to compare
N -= 1;
}
// print the array
console.log(A)
# Declare Array
A = [6,5,3,1,8,7,2,4]
# Set length
N = len(A)
# as long as there are things left to compare
while N > 0:
# put 0 into i
i = 0
# while i is less than N
while i < N-1:
# if left > right
if A[i] > A[i+1]:
# swap them
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
# increment i
i += 1
# decrease total things to compare
N -= 1
# print the array
print(A)
# Declare Array
A = [6,5,3,1,8,7,2,4]
# Set length
N = A.length()
# as long as there are things left to compare
while N > 0 do
# put 0 into i
i = 0
# while i is less than N
while i < N-1 do
# if left > right
if A[i] > A[i+1] then
# swap them
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
end
# increment i
i += 1
end
# decrease total things to compare
N -= 1
end
# print the array
print(A, "\n")
// Declare Array
var A = [6,5,3,1,8,7,2,4]
// Set length
var N = A.count
// as long as there are things left to compare
while N > 0
{
// put 0 into i
var i = 0
// while i is less than N
while i < N-1
{
// if left > right
if A[i] > A[i+1]
{
// swap them
var temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
}
// increment i
i += 1
}
// decrease total things to compare
N -= 1
}
// print the array
print(A)

Visualization

bs

Compiling and Running

Clone the repo:

git clone https://gist.github.com/vgmoose/403fee8751b920cab955
cd 403fee8751b920cab955

C

gcc c_bs.c
./a.out

Java

javac Java_BS.java
java Java_BS

Javascript

node javascript-bs.js

Python

python python-bs.py

Ruby

ruby ruby-bs.rb

Swift

swift swift-bs.swift

High level Differences

C

  • Uses // for comments
  • Uses int for integer variables
  • Uses int* for integer arrays
  • Uses () and {} for loops and blocks
  • Arrays must have their size specified
  • Has no way to print an array without a loop

Java

  • Uses // for comments
  • Uses int for integer variables
  • Uses int[] for integer arrays
  • Uses () and {} for loops and blocks

Javascript

  • Uses // for comments
  • Uses var for all variables
  • Uses () and {} for loops and blocks

Python

  • Uses # for comments
  • Uses whitespace for loops and blocks

Ruby

  • Uses # for comments
  • Uses do/then..end for blocks

Swift

  • Uses // for comments
  • Uses var for all variables
  • Uses {} for blocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.