Contents of examples.html

/* This is an example of a bunch of Chapel syntax. 
 * Authors: Kyle Burke, David Bunde
 */

use Time; //allows us to time code
use Random; //allows us to generate random arrays.

var n = 20;

var numbers : [0..n-1] real; //array declaration with indices 0, 1, ..., n-1

//initialize part of the array
numbers[0] = 2.5;
numbers[1] = 14.1;
numbers[2] = -8.2;
numbers[3] = 2.5;
numbers[4] = 1.2;

writeln("Pre-sorting Numbers: ", numbers, "\n");

//bubble sort
for i in numbers.domain {
	for j in 0..numbers.domain.numIndices-2 {
		if numbers[j] > numbers[j+1] {
			var temp = numbers[j];
			numbers[j] = numbers[j+1];
			numbers[j+1] = temp;
		}
	}
}

writeln("Sorted Numbers: ", numbers, "\n");

//Sorts array using bubbleSort
proc bubbleSort(array) {
	for i in array.domain {
	    for j in 0..array.domain.numIndices-2 {
		    if array[j] > array[j+1] {
			    var temp = array[j];
			    array[j] = array[j+1];
			    array[j+1] = temp;
		    }
	    }
    }
}

//re-initialize part of the array
numbers[0] = 2.5;
numbers[1] = 14.1;
numbers[2] = -8.2;
numbers[3] = 2.5;
numbers[4] = 1.2;

writeln("Partly re-initialized numbers: \n", numbers, "\n");

var timer = new Timer();

timer.start();
bubbleSort(numbers);
timer.stop();

writeln("Resorted numbers: \n", numbers, "\n");

writeln("That last sort took ", timer.elapsed(), " seconds!");

timer.clear();

fillRandom(numbers);
writeln("More unsorted numbers: \n", numbers, "\n");
bubbleSort(numbers);
writeln("Sorted those: \n", numbers, "\n");