RSS

shell sort di java (Shell.java)

24 Des

source code:

public class Shell {
private long[] data;

private int len;

public Shell(int max) {
data = new long[max];
len = 0;
}

 

public void masuk(long value){
data[len] = value;
len++;
}

public void tampil() {
System.out.print(“Data:”);
for (int j = 0; j < len; j++)
System.out.print(data[j] + ” “);
System.out.println(“”);
}

public void shellSort() {
int inner, outer;
long temp;
//find initial value of h
int h = 1;
while (h 0) // decreasing h, until h=1
{
// h-sort the file
for (outer = h; outer h – 1 && data[inner – h] >= temp) {
data[inner] = data[inner – h];
inner -= h;
}
data[inner] = temp;
}
h = (h – 1) / 3; // decrease h
}
}

public static void main(String[] args) {
int maxSize = 10;
Shell arr = new Shell(maxSize);

for (int j = 0; j < maxSize; j++) {
long n = (int) (java.lang.Math.random() * 99);
arr.masuk(n);
}
arr.tampil();
arr.shellSort();
arr.tampil();
}
}

Running Program :

~menyusul~

 
Tinggalkan komentar

Ditulis oleh pada 24 Desember 2011 in java programming, sorting

 

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

 
%d blogger menyukai ini: