Example of Coding on haXe (Heapsort, Binary Search) – Part 5

NOTIFICATION: These examples are provided for educational purposes. The use of this code and/or information is under your own responsibility and risk. The information and/or code is given ‘as is’. I do not take responsibilities of how they are used.

In this example you will see: Binary search by using heapsort for sorting.

example_5.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

class HeapSort{

	private var data : Array<Int>;
	private var heap_size : Int;
	private inline function leftChild(index : Int) : Int{ return (index << 1) + 1;	}
	private inline function rightChild(index : Int) : Int{ return (index << 1) + 2; }

	// Constructor ( ? means that items is optional)
	public function new(?data : Array<Int>){
		this.data = (data == null) ? new Array() : data;
	}

	public function getData() : Array<Int> {
		return data;
	}

	public function setData(data : Array<Int>) : Void {
		this.data = data;
	}

	public function sort() : Void{
		if(data.iterator().hasNext()){
			heap_size = data.length;
			buildMaxHeap();
			var index : Int = data.length - 1;
			while (index > -1){
				exchange(0, index);
				maxHeapify(0, --heap_size);
				--index;
			}
		}else{
			trace('There is no data to sort');
		}
	}

	private function buildMaxHeap() : Void{
		var index : Int = data.length >> 1;
		while (index > -1){
			maxHeapify(index, heap_size);
			--index;
		}
	}

	private function maxHeapify(index : Int, heapSize : Int) : Void{
		var largest : Int = index;
		var left : Int = leftChild(index);
		var right : Int = rightChild(index);	

		if (left < heapSize && data[left] > data[index])
			largest = left;

		if (right < heapSize && data[right] > data[largest])
			largest = right;

		if (largest != index){
			exchange(index, largest);
			maxHeapify(largest, heapSize);
		}
	}

	private function exchange(index_a : Int, index_b : Int) : Void{
		var temp_value : Int = data[index_a];
		data[index_a] = data[index_b];
		data[index_b] = temp_value;
	}

}

class BinarySearch{
	private var heapsort : HeapSort;
	private var data : Array<Int>;
	private function search(value : Int, start_index : Int, end_index : Int){
		var middle_index : Int = (start_index + end_index) >> 1;
		var was_found : Bool = false;
		if(start_index < end_index){
			if(data[middle_index] == value){
				return true;
			}else{
				if(data[middle_index] > value){
					was_found = search(value, start_index, middle_index);
				}else{
					was_found = search(value, middle_index + 1, end_index);
				}
			}
		}
		return was_found;
	}

	// Constructor ( ? means that items is optional)
	public function new(?data : Array<Int>){
		this.data = (data == null) ? new Array() : data;
		if (data != null) setData(data);
	}

	public function getData() : Array<Int> {
		return data;
	}

	public function setData(data : Array<Int>) : Void {
		if (data != null){
			heapsort = new HeapSort();
			heapsort.setData(data);
			heapsort.sort();
			data = heapsort.getData();
		}
	}

	public function doExist(value : Int) : Bool{
		return search(value, 0, data.length);
	}

}

class Example_5 {

	static function main(){

		var data : Array<Int> = [3, 7, 5, -1, 6, 1, -3];
		printData(data);

		trace('Binary Search:');
		var binarySearch = new BinarySearch(data);
		trace ('Do 3 exist? ' + binarySearch.doExist(3));
		trace ('Do 4 exist? ' + binarySearch.doExist(4));
		trace ('Do 7 exist? ' + binarySearch.doExist(7));

		data = binarySearch.getData();
		printData(data);		

	}

	private static function printData(data : Array<Int>){
		var iter = data.iterator();
		var index : Int = 0;
		trace('[DATA]');
		while (iter.hasNext()){
			trace('data[' + index++ + ']: ' + iter.next());
		}
	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe (Heapsort) – Part 4

In this example you will see: Heapsort algorithm.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

example_4.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

class HeapSort{

	private var data : Array<Int>;
	private var heap_size : Int;
	private inline function leftChild(index : Int) : Int{ return (index << 1) + 1;	}
	private inline function rightChild(index : Int) : Int{ return (index << 1) + 2; }

	// Constructor ( ? means that items is optional)
	public function new(?data : Array<Int>){
		this.data = (data == null) ? new Array() : data;
	}

	public function getData() : Array<Int> {
		return data;
	}

	public function setData(data : Array<Int>) : Void {
		this.data = data;
	}

	public function sort() : Void{
		if(data.iterator().hasNext()){
			heap_size = data.length;
			buildMaxHeap();
			var index : Int = data.length - 1;
			while (index > -1){
				exchange(0, index);
				maxHeapify(0, --heap_size);
				--index;
			}
		}else{
			trace('There is no data to sort');
		}
	}

	private function buildMaxHeap() : Void{
		var index : Int = data.length >> 1;
		while (index > -1){
			maxHeapify(index, heap_size);
			--index;
		}
	}

	private function maxHeapify(index : Int, heapSize : Int) : Void{
		var largest : Int = index;
		var left : Int = leftChild(index);
		var right : Int = rightChild(index);	

		if (left < heapSize && data[left] > data[index])
			largest = left;

		if (right < heapSize && data[right] > data[largest])
			largest = right;

		if (largest != index){
			exchange(index, largest);
			maxHeapify(largest, heapSize);
		}
	}

	private function exchange(index_a : Int, index_b : Int) : Void{
		var temp_value : Int = data[index_a];
		data[index_a] = data[index_b];
		data[index_b] = temp_value;
	}

}

class Example_4 {

	static function main(){

		var data : Array<Int> = [3, 7, 5, -1, 6, 1];
		printData(data);

		trace('heapsort:');
		var heapsort = new HeapSort(data);
		trace('sorting...');
		heapsort.sort();
		data = heapsort.getData();
		printData(data);		

		data = [3, 7, 5, -1, 6, 1];
		printData(data);		

		trace('heapsort_2:');
		var heapsort_2 = new HeapSort();
		trace('sorting without data...');
		heapsort_2.sort();
		trace('sorting with data...');
		heapsort_2.setData(data);
 		heapsort_2.sort();
 		printData(data);

	}

	private static function printData(data : Array<Int>){
		var iter = data.iterator();
		var index : Int = 0;
		trace('[DATA]');
		while (iter.hasNext()){
			trace('data[' + index++ + ']: ' + iter.next());
		}
	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe (Dynamic and Recursive Fibonacci) – Part 3

In this example, you will see: private static function, recursive fibonacci and dynamic fibonacci algorithm.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

example_3.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

class Example_3 {

	private static var fib_array : Array<Int> = [1, 1];

	static function main(){
		trace('main()');
		trace('Recursive Fibonacci (value: 5): ' + recursive_fib(5));
		trace('Dynamic Fibonacci (value: 5): ' + dynamic_fib(5));
		print_array();
	}

	// Recursive Fibonacci
	private static function recursive_fib(value : Int) : Int {
		if (value < 2)
			return 1;
		return recursive_fib(value - 1) + recursive_fib(value - 2);
	}

	// Dynamic Fibonacci
	private static function dynamic_fib(value : Int) : Int {
		if (value < 2)
			return 1;
		if (fib_array[value] == 0)
			fib_array[value] = dynamic_fib(value - 1) + dynamic_fib(value - 2);
		return fib_array[value];
	}

	private static function print_array() : Void{
		var iter = fib_array.iterator();
		while (iter.hasNext()){
			trace('Array Value: ' + iter.next());
		}
	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe – Part 2

In this example you will see: For loop, while loop, do while loop, break, continue, array.reverse.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example (SWF):

Sorry, either Adobe flash is not installed or you do not have it enabled

Code example: example2.hx:

class Example_2 {

	static function main(){

		//In haxe, arrays are iterable which mean that have methods and properties
		var integer_array : Array<Int> = [1,2,3,4];

		var num : Int = 4;
		for (i in 0...num)
			trace('1) integer_array[' + i + ']: ' + integer_array[i]);

		integer_array.reverse();

		for (i in 0...num)
			trace('2) integer_array[' + i + ']: ' + integer_array[i]);

		// While loop
		var count : Int = 0;
    while(count < 5){
			trace('3) While Count: ' + count);
    	++count;
    }

		count = 0;
		do{
			trace('4) Do While Count: ' + count);
    	count++;
    }while(count < 5);

		// continue - skip interation and go backi to while condition
		count = -1;
		while( count < 5 ) {
			++count;
			trace('5) While with continue Count: ' + count);
		  if( count == 3 ){
				trace('5) CONTINUE go to while');
	      continue;
		  }
		}
		trace('5) While with continue ends at count: ' + count);			

		// break - Jump out of the 'while' loop, and continue
		count = -1;
		while( count < 5 ) {
			++count;
			trace('6) While with break Count: ' + count);
		  if( count == 3 ){
				trace('6) Break!!!');
	      break;
		  }
		}
		trace('6) While with break ends at count: ' + count);			

  }

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe – Part 1

In this example, your will see: Trace, enum, variables (integer, boolean, void, string, float, dynamic), null, array iterable, for loop, if/else.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example:
Sorry, either Adobe flash is not installed or you do not have it enabled

Code Example: Example_1.hx

// enum go outside the class
enum Enum_Colors {
	blue;
	red;
	yellow;
}

class Example_1 {

	static function main(){

		// Integers
		var variable_integer : Int;
		variable_integer = 0;
		trace('1) variable_integer = ' + variable_integer);
		variable_integer = -134;
		trace('2) variable_integer = ' + variable_integer);
  	variable_integer = 0xFF00;
		trace('3) variable_integer = ' + variable_integer);

		// Float
		var variable_float : Float;
	  variable_float = 123.0;
		trace('4) variable_float = ' + variable_float);
	  variable_float = .14179;
		trace('5) variable_float = ' + variable_float);
	  variable_float = 13e50;
		trace('6) variable_float = ' + variable_float);
	  variable_float = -1e-99;
		trace('7) variable_float = ' + variable_float);

		// String
		var variable_string : String;
	  variable_string = 'hello';
		trace('8) variable_string = ' + variable_string);
    variable_string = 'hello \'world\' !';
    trace('9) variable_string = ' + variable_string);
	  variable_string = 'hello 'world' !';
	  trace('10) variable_string = ' + variable_string);

		// Boolean
		var variable_bool : Bool;
	  variable_bool = true;
	  trace('11) variable_bool = ' + variable_bool);
  	variable_bool = false;
		trace('12) variable_bool = ' + variable_bool);

	  // Dynamic
	  var variable_dynamic : Dynamic;
	  variable_dynamic = 7;
	  trace('13) variable_dynamic = ' + variable_dynamic);
	  variable_dynamic = 'Now, variable_dynamic holds a string';
	  trace('14) variable_dynamic = ' + variable_dynamic);

	  // Assigning null
	  var variable;
	  variable = null;
	  trace('15) variable = ' + variable);

		// Using enum constants
		var variable_colors : Enum_Colors;
		variable_colors = blue;
		if (variable_colors == blue){
			trace('16) variable_colors = blue');
		}else{
			trace('16) variable_colors is not blue');
		}

		variable_colors = yellow;
		if (variable_colors == blue){
			trace('17) variable_colors = blue');
		}else{
			trace('17) variable_colors is not blue');
		}

		// Using Arrays (in haxe, arrays are iterable)
		var integer_array : Array<Int> = [1,2,3,4];

		// You can loop through integer_array's members directly with a for..in loop
		var count : Int = 0;
		for (i in integer_array){
			trace('18) integer_array[' + count++ + ']: ' + i);
		}

		var num : Int = 4;
		for (i in 0...num)
			trace('19) integer_array[' + i + ']: ' + integer_array[i]);

		for (i in 0...4)
			trace('20) integer_array[' + i + ']: ' + integer_array[i]);

		// This would not work: for (i in 3...0)
		var i : Int = 3;
		while (i >= 0){
			trace('21) integer_array[' + i + ']: ' + integer_array[i]);
			--i;
		}

  }

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment