shellsort.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
$warnings 3  # Set warning level 3
clear();
numeric ArraySize=42;
array numeric a[ArraySize];
a[1] = 145;
a[2] = 1345;
a[3] = 1322;
a[4] = 023;
a[5] = 045;
a[6] = 9;
a[7] = 555;
a[8] = 55;
a[9] = 8;
a[10] = 0;
a[11] = 14;
a[12] = 135;
a[13] = 22;
a[14] = 0213;
a[15] = 1045;
a[16] = 98;
a[17] = 5545;
a[18] = 5;
a[19] = 81;
a[20] = 10;
a[21] = 9;
a[22] = 555;
a[23] = 55;
a[24] = 8;
a[25] = 0;
a[26] = 14;
a[27] = 135;
a[28] = 22;
a[29] = 8;
a[30] = 10;
a[30] = 110;
a[31] = 19;
a[32] = 1555;
a[33] = 155;
a[34] = 18;
a[35] = 10;
a[36] = 114;
a[37] = 1135;
a[38] = 122;
a[39] = 18;
a[40] = 10;
a[41] = 99999;
a[42] = 0;
numeric i;
func shellsort(first, last)
{
	numeric h=1;
	
	while ((h * 3 + 1) < last-1)
	{
		h = 3 * h + 1;
		print("h=",h);
	}
		
	while (h > 0)
	{
		# for each of the h sets of elements
		for i = h-1 to last
		{
			numeric key = a[i];
			numeric j = i;
			
			while (j>=h && a[j-h] > key)
			{
				a[j] = a[j-h];	
				j = j - h;	
			}
		
			a[j] = key;
		}
		h = floor(h/3);	
	}
}
shellsort(1, a.numItems);
for i=1 to a.numItems
{
	print(i, a[i]); 
}