bubble sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by Wayfur

in haxe

Source Code

package ;

/**
 * Haxe implementation of bubble sort
 * 
 * Bubble sort is a simple sorting algorithm that works
 * by repeatedly stepping through the listto be sorted,
 * comparing each pair of adjacent items 
 * and swapping them if they are in the wrong order. 
 * The pass through the list is repeated until no swaps are needed.
 * 
 * @author Tyler Pond AKA Wayfur
 */
class BubbleSort
{

	public function new(values:Array<Int>) 
	{
		trace(bubbleSort(values));
	}
	
	public function bubbleSort(arr:Array<Int>):Array<Int>
	{
		var isSorted:Bool = false;
		var temp:Int;
		while (!isSorted)
		{
			isSorted = true;
			for (i in 1...arr.length)
			{
				if (arr[i] < arr[i - 1])
				{
					temp = arr[i];
					arr[i] = arr[i - 1];
					arr[i - 1] = temp;
					isSorted = false;
				}
			}
		}
		return arr;
	}
}
package ;
import neko.Lib;
import Factorial;

/**
 * @author Tyler Pond AKA Wayfur
 */

class Main 
{
	
	static function main() 
	{
		var bubble = new BubbleSort([999,47,12,1,36,987,677,513,514,654,412]);
		
		#if neko
			Lib.print(bubble);
		#else
			trace(bubble);
		#end
	}
	
}