/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mergesort;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
//import java.io.InputStreamReader;
/**
*
* @author bapake-aisyah
*/
public class MergeSort {
String[][] nama = new String[220][3];
String temp[];
private Scanner z;
public MergeSort() {
}
public void setNama(String[] nama) {
}
public void mergesort(String[][] nama, int l, int h) {
int bwh = l;
int ats = h;
int tgh = (h + l) / 2;
if (l >= h) {
return;
}
int m = tgh;
int n = tgh + 1;
mergesort(nama, l, m);
mergesort(nama, n, h);
while (l <= m && n <= h) {
if (nama[l][0].substring(0, 1).compareTo(nama[n][0].substring(0, 1))<0) {
l++;
} else {
String temp1 = nama[n][0];
String temp2 = nama[n][1];
String temp3 = nama[n][2];
for (int i = n - 1; i >= l; i--) {
nama[i + 1][0] = nama[i][0];
nama[i + 1][1] = nama[i][1];
nama[i + 1][2] = nama[i][2];
}
nama[l][0] = temp1;
nama[l][1] = temp2;
nama[l][2] = temp3;
l++;
m++;
n++;
}
}
}
public void mergesortblk(String[][] nama, int l, int h) {
int bwh = l;
int ats = h;
int tgh = (h + l) / 2;
if (l >= h) {
return;
}
int m = tgh;
int n = tgh + 1;
mergesortblk(nama, l, m);
mergesortblk(nama, n, h);
while (l <= m && n <= h) {
//if ((int) nama[l][0].substring(1, 2).charAt(0) < (int) nama[n][0].substring(1, 2).charAt(0)) {
if (nama[l][1].substring(0, 1).compareTo(nama[n][1].substring(0, 1))<0) {
l++;
} else {
String temp1 = nama[n][0];
String temp2 = nama[n][1];
String temp3 = nama[n][2];
//String temp2 = nama[n][1];
for (int i = n - 1; i >= l; i--) {
//for (int j = 0; j<2; j++) {
nama[i + 1][0] = nama[i][0];
nama[i + 1][1] = nama[i][1];
nama[i + 1][2] = nama[i][2];
//}
}
nama[l][0] = temp1;
nama[l][1] = temp2;
nama[l][2] = temp3;
//nama[l][1] = temp2;
l++;
m++;
n++;
}
}
}
public void tampil(String dir) throws IOException {
File file = new File(dir);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int i = 0;
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public String[] array(String dir) throws IOException {
temp = new String[220];
for (int i=0; i<220; i++) {
temp[i] = "";
}
//int i=0;
try {
FileReader fr = new FileReader(dir);
BufferedReader br = new BufferedReader(fr);
String isiFile = null;
System.out.println("Isi file " + dir + " :");
System.out.println("---------------");
for (int j=0; j<220; j++) {
if((isiFile = br.readLine()) != null){
temp[j]=isiFile;
}
}
}catch(FileNotFoundException e){
}
return temp;
}
public void pisahKata(String[] array) {
for (int i = 0; i < 220; i++) {
for (int j = 0; j < 3; j++) {
this.nama[i][j] = "";
}
}
try {
this.array("Data Nama.txt");
for (int i = 0; i < temp.length; i++) {
//for (int j=0; j<2; j++){
if (temp[i] != "") {
String smtr = temp[i];
z = new Scanner(smtr);
z.useDelimiter(" ");
int j = 0;
while (z.hasNext()) {
nama[i][j] = z.next();
j++;
}
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void mBalik(String[][] input) {
for (int i = 0; i < input.length; i++) {
if (input[i][1] == null ? "" == null : input[i][1].equals("")) {
//String temp =
input[i][1] = input[i][0];
}
String temp = input[i][0];
input[i][0] = input[i][1];
input[i][1] = temp;
}
}
public void buatFile(String namaFile) {
File f = new File(namaFile + ".txt");
System.out.println("Nama File = " + f.getName());
if (f.exists()) {
System.out.println("File sudah ada.");
} else {
System.out.println("File belum ada, membuat baru.");
try {
if (f.createNewFile()) {
System.out.println("Selesai.");
} else {
System.out.println("Gagal.");
//System.exit(0);
} }catch(IOException e) {
}
}
FileOutputStream fos;
DataOutputStream dos;
String n = "";
try {
File file = new File(namaFile +".txt");
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
for (int i=0; i<nama.length; i++) {
for (int j=0; j<3; j++) {
n += nama[i][j] + " ";
}
n+= "\n";
}
dos.writeUTF(n);
} catch (IOException e) {
e.printStackTrace();
}
}
/*public static void main(String[] a) {
MergeSort baru = new MergeSort();
try {
baru.tampil("Data Nama.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("====================");
try{
baru.array("Data Nama.txt");
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("====================");
baru.pisahKata(baru.temp);
for (int i=0; i<220; i++) {
for (int j= 0; j<2; j++) {
if (baru.nama[i][j] != "")
System.out.print(baru.nama[i][j]+ " ");
}
System.out.println("");
}
String x = "KETIKA";
String y = "AKU";
if ((x.substring(0, 1).compareTo(y.substring(0,1))) < 0) {
System.out.println(" x duluan ");
}else{
System.out.println("y duluan");
}
System.out.println("====================");
System.out.println("====================");
baru.mBalik(baru.nama);
baru.mergesort(baru.nama, 0, baru.nama.length-1);
baru.mBalik(baru.nama);
for (int i=0; i<220; i++) {
for (int j= 0; j<3; j++) {
if (baru.nama[i][j] != "")
System.out.print(baru.nama[i][j]+ " ");
}
System.out.println("");
}
/*String smtr = baru.temp[2];
String []test = smtr.split(" ");
System.out.println(test[0]);
System.out.println(test[1]);
baru.nama[1][0]=test[0];
baru.nama[1][1]=test[1];
baru.nama[1][2]=test[2];
System.out.println(baru.nama[1][0]);
System.out.println(baru.nama[1][1]);
System.out.println(baru.nama[1][2]);*/
//}
}
kok kurang lengkap sih.... gag ada main methodnya.........
BalasHapusada kok, cuman dikasih tnda 'komentar'
BalasHapusom programnya rada kadaluarsa ya????
BalasHapuskok nggak jalan bro?
BalasHapusdi buka dulu tanda komentar buat main-nya bro
BalasHapus