Coding Insertion sort dengan konsep OOP dan inputan user,,,,,,
Semoga berhasil yha teman,,,,
import java.io.*;
import javax.swing.JOptionPane;
class insert{
static void printArray(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
System.out.print("\n");
}
public static void insertion_sort(int array[], int n){
for (int i = 0; i < n; i++){
int j = i;
int B = array[i];
while ((j > 1) && (array[j-1] > B)){
array[j] = array[j-1];
j--;
}
array[j] = B;
} }
public static void main (String args []) throws IOException{
String get_value;
int n =0;
String strEntry;
int i=0;
int x = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Index nya:"));
int entry[] = new int[x];
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.println();
for( i =0 ; i < entry.length; i++){
System.out.print("Masukkan Angka nya " + i +" : ");
strEntry = dataIn.readLine();
entry[i] = Integer.parseInt(strEntry);
}
System.out.println();
System.out.println("== Angka Asli ==>");
printArray(entry);
insertion_sort(entry,entry.length );
System.out.println("\n");
System.out.println("== Angka Setelah Diurutkan ==>");
printArray(entry);
}}
Semoga berhasil yha teman,,,,
import java.io.*;
import javax.swing.JOptionPane;
class insert{
static void printArray(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
System.out.print("\n");
}
public static void insertion_sort(int array[], int n){
for (int i = 0; i < n; i++){
int j = i;
int B = array[i];
while ((j > 1) && (array[j-1] > B)){
array[j] = array[j-1];
j--;
}
array[j] = B;
} }
public static void main (String args []) throws IOException{
String get_value;
int n =0;
String strEntry;
int i=0;
int x = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Index nya:"));
int entry[] = new int[x];
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.println();
for( i =0 ; i < entry.length; i++){
System.out.print("Masukkan Angka nya " + i +" : ");
strEntry = dataIn.readLine();
entry[i] = Integer.parseInt(strEntry);
}
System.out.println();
System.out.println("== Angka Asli ==>");
printArray(entry);
insertion_sort(entry,entry.length );
System.out.println("\n");
System.out.println("== Angka Setelah Diurutkan ==>");
printArray(entry);
}}