Instantly share code, notes, and snippets.

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

Bubble sort in multiple languages
 #include #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
 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)

## 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 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 int for integer variables
• Uses int[] for integer arrays
• Uses () and {} for loops and blocks

### Javascript

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

### Python

• Uses whitespace for loops and blocks