How to create a bubble sort with Java and Haxe
There are tones of posts online that cover bubble sort. Lately I’ve been brushing up on my Java and also I like Haxe so here are two videos covering bubble sort in each of the two languages.
Java Bubble Sort Algorithm
import java.util.Arrays; /** * Created by matthew.wallace on 2/10/17. */ public class Main { public static void main(String[] args) { int[] list = {4,6,2,90,245,3,21,356,42}; System.out.println("before sort : " + Arrays.toString(list)); int i,j,temp; for (i = 0; i < list.length - 1 ; i++) { for (j = 0; j < list.length - 1 - i ; j++) { if( list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } System.out.println("after sort : " + Arrays.toString(list)); } }
Haxe Bubble Sort Algorithm
package ; class Main { public function new() { var list:Array<Int> = [7,5,1,2,45,37,42,183,3]; var temp:Int; trace("unsorted list : "+ list); for(i in 0...list.length - 1) { for(j in 0...list.length - 1 - i) { if ( list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } trace("sorted list : "+ list); } public static function main() { new Main(); } }