Insert Short with Java


Insert Short

Ini adalah lanjutan dari materi yang kemarin,mengenai Buble Short.

Dan kali ini kita akan membuat saudara nya dari Bubble Short yaitu InsertShort

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class insertionSort {

//banyak elemen array integer

final static int Nmax= 10;

public static void insertionSort(int L[]){

for (int i=Nmax-1;i>=1;i–) {

for (int j=i-1;j>=0;j–) {

if (L[i]<L[j]) {

int c = L[i];

L[i] = L[j];

L[j] = c;

}

}

}

}

public static void main (String args []) {

BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

int L[] = new int [Nmax];

try {

//Menerima inputan sebanyak Nmax kali

for (int i=0;i<Nmax;i++) {

System.out.print(“Enter an integer”);

L[i] = Integer.parseInt(in.readLine());

}

//Insertion_Ascending(L);

insertionSort(L);

for (int i=0;i<Nmax;i++) {

System.out.print(L[i]+” , “);

}

}

catch (Exception e) {

System.out.println(“ERROR ada kesalahan dalam input”);

}

}

}

3 thoughts on “Insert Short with Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s