Java Zone

Pemrograman java, Perulangan Java, Operator java, Thread Java, Makalah Filsafat Ilmu, Makalah Teologi dan lain lain

Powered by Blogger.

Source Code Program Java Sorting BubbleSort, SelectionSort, InsertionSort

SORTING – NON GUI

Nama program:  bubbleSort.java

public class BubbleSort {
public static void main(String[] args) {
int array[] = new int[5];
array[0] = 7;
array[1] = 5;
array[2] = 3;
array[3] = 8;
array[4] = 2;
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println(“”);
int kanan;
int kiri;
for(kanan=array.length-1; kanan>0; kanan–) {
for(kiri=0; kiri<kanan; kiri++) {
if( array[kiri] > array[kiri+1] ) {
int temp = array[kiri];
array[kiri] = array[kiri+1];
array[kiri+1] = temp;
}
}
}
Output program:
7 5 3 8 2
2 3 5 7 8

Nama program: SelectionSort.java

public class SelectionSort {
public static void main(String[] args) {
int array[] = new int[5];
for(int index=0; index<array.length; index++) {
array[index] = (int) (Math.random()*100);
}
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println();
int x, y, min;
for(x=0; x<array.length; x++) {
min = array[x];
for(y=x+1; y<array.length; y++) {
if(array[y] < array[x]) {
min = array[y];
int temp = min;
array[y] = array[x];
array[x] = temp;
}
}
}
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println();
}
}
Output program:
9 69 96 97 47
9 47 69 96 97

Nama program: InsertionSort.java

public class InsertionSort {
public static void main(String[] args) {
int array[] = new int[5];
array[0] = 7;
array[1] = 5;
array[2] = 3;
array[3] = 8;
array[4] = 2;
int a;
int b;
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println(“”);
for(a=1; a<array.length; a++)
{
int temp = array[a];
b = a;
while(b>0 && array[b-1] >= temp)
{
array[b] = array[b-1];
–b;
}
array[b] = temp;
}
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println(“”);
}
}
Output program:
7 5 3 8 2
2 3 5 7 8


SORTING – GUI

Nama program: SortingGui.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SortingGui {
JPanel panelTengah = new JPanel();
JPanel panelKanan = new JPanel();
JPanel panelBawah = new JPanel();
JPanel panelSemua = new JPanel();
JTextField[] fieldArray = new JTextField[10];
JLabel[] label = new JLabel[10];
JTextField sisipElemen = new JTextField(5);
JButton buttonSisip = new JButton(“Sisip”);
JTextField hapusElemen = new JTextField(5);
JButton buttonHapus = new JButton(“Hapus”);
JButton buttonLinearSort = new JButton(“Linear Sort”);
JButton buttonInsertionSort = new JButton(“Insertion Sort”);
JButton buttonBubbleSort = new JButton(“Bubble Sort”);
SortingGui() {
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl = new GridLayout(10, 2);
panelTengah.setLayout(gl);
for (int x = fieldArray.length-1; x >=0; x–) {
panelTengah.add(label[x] = new JLabel(Integer.toString(x)));
panelTengah.add(fieldArray[x] = new JTextField(5));
}
panelSemua.add(“Center”, panelTengah);
GridLayout gl2 = new GridLayout(2, 2);
panelKanan.setLayout(gl2);
panelKanan.add(sisipElemen);
panelKanan.add(buttonSisip);
panelKanan.add(hapusElemen);
panelKanan.add(buttonHapus);
panelSemua.add(“North”, panelKanan);
GridLayout gl3 = new GridLayout(2, 2);
panelBawah.setLayout(gl3);
panelBawah.add(buttonLinearSort);
panelBawah.add(buttonInsertionSort);
panelBawah.add(buttonBubbleSort);
panelSemua.add(“South”, panelBawah);
JFrame jendela = new JFrame(“Sorting GUI”);
jendela.setContentPane(panelSemua);
//jendela.setSize(200,400);
//jendela.setResizable(false);
jendela.pack();
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
ProsesSorting proses = new ProsesSorting(this);
buttonSisip.addActionListener(proses);
buttonHapus.addActionListener(proses);
buttonLinearSort.addActionListener(proses);
buttonInsertionSort.addActionListener(proses);
buttonBubbleSort.addActionListener(proses);
}
public static void main(String[] args) {
SortingGui sort = new SortingGui();
}
}

Nama program: ProsesSorting

import java.awt.Color;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class ProsesSorting implements ActionListener {
SortingGui induk;
int array[] = new int[10];
int nElemen = 0;
ProsesSorting(SortingGui induk) {
this.induk = induk;
}
public void actionPerformed(ActionEvent kejadian) {
if(kejadian.getSource() == induk.buttonSisip) {
array[nElemen] = Integer.parseInt(induk.sisipElemen.getText());
//int sisip = Integer.parseInt(induk.sisipElemen.getText());
induk.fieldArray[nElemen].setText(Integer.toString(array[nElemen]));
induk.fieldArray[nElemen].setBackground(Color.green);
induk.sisipElemen.setText(“”);
nElemen++;
tampil();
}
else if(kejadian.getSource() == induk.buttonHapus){
int j;
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(“”);
}
for(j=0; j<nElemen; j++){
if( Integer.parseInt(induk.hapusElemen.getText()) == array[j] )
break;
}
if(j==nElemen)
JOptionPane.showMessageDialog(null, “tidak ada”);
else
{
for(int k=j; k<nElemen; k++) {
array[k] = array[k+1];
//nElemen–;
}
nElemen–;
induk.fieldArray[nElemen].setBackground(Color.white);
}
induk.hapusElemen.setText(“”);
tampil();
} else if(kejadian.getSource() == induk.buttonLinearSort) {
for(int x = 0; x<nElemen-1; x++) {
for(int y = x+1; y<nElemen; y++) {
if(array[x] > array[y]){
int temp = array[x];
array[x] = array[y];
array[y] = temp;
}
}
}
tampil();
} else if(kejadian.getSource() == induk.buttonInsertionSort) {
int in, out;
for(out=1; out<nElemen; out++)
{
int temp = array[out];
in = out;
while(in>0 && array[in-1] >= temp)
{
array[in] = array[in-1];
–in;
}
array[in] = temp;
}
tampil();
} else if(kejadian.getSource() == induk.buttonBubbleSort) {
for (int x = nElemen-1; x > 1; x–) {
for (int y = 0; y < x; y++) {
if (array[y] > array[y + 1]) {
int temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
}
}
}
tampil();
}
}
public void tampil(){
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(” “);
}
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(Integer.toString(array[x]));
}
}
}
Output program:
Elemen array sebelum diurutkan
Elemen array sebelum diurutkan
Elemen array setelah diurutkan
Elemen array setelah diurutkan
Bagikan :
+
Previous
Next Post »
0 Komentar untuk "Source Code Program Java Sorting BubbleSort, SelectionSort, InsertionSort"

 
Copyright © 2015 Java Zone - All Rights Reserved
Template By Kunci Dunia
Back To Top