selection sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by Wayfur

in haxe

Source Code

package ;

/**
 * Haxe implementation of Selection Sort
 * 
 * @author Tyler Pond AKA Wayfur
 */
class SelectionSort
{

	public function new(arr:Array<Int>) 
	{
		trace(selectionSort(arr));
	}
	
	private function selectionSort(arr:Array<Int>):Array<Int>
	{
		var sorted:Array<Int> = arr;
		for (i in 0...sorted.length)
		{
			var minIndex = i;
			for (remaining in (i+1)...sorted.length)
			{
				if (sorted[minIndex] > sorted[remaining]) {
					minIndex = remaining;
				}
			}
			if (i != minIndex) {
				var temp = sorted[i];
				sorted[i] = sorted[minIndex];
				sorted[minIndex] = temp;
			}
		}
		return sorted;
	}
}
package ;
import neko.Lib;
import Factorial;

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

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