Friday, December 17, 2010

Contoh-contoh Program Sorting dalam Nusaptel

imageDalam kesempatan kali ini saya akan menampilkan source code algoritma sorting dalam bahasa nusaptel. Hanya versi Nusaptel terbaru yang mendukung compile program-program ini. (Nusaptel IPT anda harus mendukung pointer dan rekursi ) Anda dapat mendapatkan Nusaptel terbaru di : www.bernaridho.net 

  • Insertion Sort :
Program insertionSort;
void insertionSort( shortword[] data)
{
	shortword i,j,val;
	for(i := 2;i<=length(data);i++)
	{
		val := data[i];
		j := i;
		while( j>1 AND data[j-1]>val)
		{
			data[j] := data[j-1];
			j--;
		}
		data[j] := val;
	}
	
}
void main()
{
shortword[] data := {3,4,1,9,2,5,10,0,12,7,11,6};
shortword i;
writeline('sebelum di sorting :');
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
writeline('');
writeline('stelah disorting :');
insertionSort(data);
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
}


  • Selection Sort:
Program selectionSort;
void swap( shortword[] &array; shortword i; shortword j)
{
	shortword temp := array[i];
	array[i] := array[j];
	array[j] := temp;
}
void selectionSort(shortword[] array)
{
	shortword min, i, j;
	for(i:=1;i<length(array)-1;i++)
	{
		min := i;
		for(j:=i+1;j<=length(array);j++)
			if(array[j] < array[min])
				min := j;
		
		if(min!=i)
			swap(array,min,i);
	}
	
}
void main()
{
shortword[] data := {3,4,1,9,2,5,10,0,12,7,11,6};
shortword i;
writeline('sebelum di sorting :');
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
writeline('');
writeline('stelah disorting :');
selectionSort(data);
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
}


  • Quick Sort:
Program quickSort;
void swap( shortword[] &array; shortword i; shortword j)
{
	shortword temp := array[i];
	array[i] := array[j];
	array[j] := temp;
}
shortword partition(shortword[] array; shortword left; shortword right)
{
shortword i := left, j := right;
shortword temp;
shortword pivot := array[(left+right)%2];
while(i<=j)
{
	while(array[i] < pivot)	
		i++;
	while(array[j] > pivot)
		j--;
	if( i <= j )
	{
		swap(array,i,j);
		i++; j--;
	}
}
return(i);
}
void quickSort(shortword[] array; shortword left; shortword right)
{
	shortword index := partition(array,left,right);
	if(left < index - 1)
		quickSort(array,left,index-1);
	if(index < right)
		quickSort(array,index,right);
}
 
void main()
{
shortword[] data := {3,4,1,9,2,5,10,0,12,7,11,6};
shortword i;
writeline('sebelum di sorting :');
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
writeline('');
writeline('stelah disorting :');
quickSort(data, 1, length(data));
for(i := 1; i<= length(data);i++)
{
	write(' ',data[i]);
}
}



  • Bubble Sort
Program bubbleSort;
void swap( shortword[] &array; shortword i; shortword j)
{
  shortword temp := array[i];
  array[i] := array[j];
  array[j] := temp;
}
void bubbleSort(shortword[] data)
{
  shortword i,j;
  for(j:=1;j<length(data);j++)
    for(i:=1;i<length(data);i++)
      if(data[i+1]<data[i])
        swap(data,i,i+1);
}
void main()
{
shortword[] data := {3,4,1,9,2,5,10,0,12,7,11,6};
shortword i;
writeline('sebelum di sorting :');
for(i := 1; i<= length(data);i++)
{
  write(' ',data[i]);
}
writeline('');
writeline('stelah disorting :');
bubbleSort(data);
for(i := 1; i<= length(data);i++)
{
  write(' ',data[i]);
}
  
}

image


Semua program akan menghasilkan hasil seperti gambar di atas.


Selamat mencoba.

No comments:

Post a Comment