MERGE SORT
import java.io.BufferedReader;import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author agintageniusa
*/
public class Main {
public static void main(String[] args) {
BufferedReader buffered = new BufferedReader(new InputStreamReader(System.in));
baca_file baca = new baca_file();
System.out.println("masukan alamat file yang ingin dibaca ?");
//D:\latihan_java\5210105008_Praktiku_7\DataNama.txt
try {
String namaFile = buffered.readLine();
baca.setnamaFile(namaFile);
} catch (IOException exp) {
System.out.println("coba lagi !");
}
System.out.println("\nMasukan pilihan anda ?");
System.out.println("1. mengurutkan dari depan.");
System.out.println("2. mengurutkan dari belakang");
System.out.println("3. Keluar");
Scanner scan = new Scanner(System.in);
int pilihan = scan.nextInt();
if (pilihan == 1) {
baca.bacaFileDepan();
String[] files = {"DataNama.txt"};
for (int x = 0; x < files.length; x++) {
mergesort text = new mergesort();
String[] sortTest = text.mergesort(baca.getarrayIsiFile());
for (int d = 0; d < sortTest.length; d++) {
if (d == sortTest.length - 1) {
System.out.println(sortTest[d] + ".");
} else {
System.out.println(sortTest[d] + ";");
}
}
System.out.print("\n");
}
System.exit(0);
} else if (pilihan == 2) {
baca.bacaFileBelakang();
String[] files = {"DataNama.txt"};
for (int x = 0; x < files.length; x++) {
mergesort text = new mergesort();
String[] sortTest = text.mergesort(baca.getarrayIsiFile());
for (int d = 0; d < sortTest.length; d++) {
if (d == sortTest.length - 1) {
System.out.println(sortTest[d] + ".");
} else {
System.out.println(sortTest[d] + ";");
}
}
System.out.print("\n");
}
System.exit(0);
} else if (pilihan == 3) {
System.exit(0);
} else {
System.out.println("pilihan salah !");
}
}
}
----------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author agintageniusa
*/
public class baca_file {
private String isiFile="", namaFile="";
private int penghitung=0;
private String [] arrayIsiFile= new String[220];
public String getisiFile() {
return isiFile;
}
public String getnamaFile() {
return isiFile;
}
public int getPenghitung() {
return penghitung;
}
public String [] getarrayIsiFile() {
return arrayIsiFile;
}
public void setnamaFile(String x) {
namaFile = x;
}
public void printarrayIsiFile(){
for (int i=0;i<arrayIsiFile.length;i++){
if (i==arrayIsiFile.length-1){
System.out.print(arrayIsiFile[i]);
} else {
System.out.print(arrayIsiFile[i]+";");
}
}
}
public void bacaFileDepan(){
try {
String isiFileini="";
FileReader bacaNamaFileIni=new FileReader(namaFile);
BufferedReader bacaFileIni=new BufferedReader(bacaNamaFileIni);
isiFile="";
System.out.println("isi file "+namaFile+ " adalah :");
System.out.println("------------------------------");
while ((isiFileini=bacaFileIni.readLine())!=null){
isiFile+=isiFileini+";";
arrayIsiFile[penghitung]=isiFileini;
penghitung++;
}
bacaNamaFileIni.close();
bacaFileIni.close();
} catch (FileNotFoundException exp){
System.out.println("File tidak ada !");
System.exit(1);
} catch (IOException exp){
System.out.println("Coba Lagi");
System.exit(1);
}
}
public void bacaFileBelakang(){
try {
String isiFileini="";
FileReader bacaNamaFileIni=new FileReader(namaFile);
BufferedReader bacaFileIni=new BufferedReader(bacaNamaFileIni);
isiFile="";
System.out.println("isi file "+namaFile+ " adalah :");
System.out.println("------------------------------");
while ((isiFileini=bacaFileIni.readLine())!=null){
isiFile+=isiFileini+";";
Scanner scanner1=new Scanner(isiFileini);
String isiFile1=scanner1.next();
String isiFile2="";
if (scanner1.hasNext()){
isiFile2=scanner1.nextLine();
} else {
isiFile2=" "+isiFile1;
}
arrayIsiFile[penghitung]=(isiFile2+","+isiFile1);
penghitung++;
}
bacaNamaFileIni.close();
bacaFileIni.close();
} catch (FileNotFoundException exp){
System.out.println("File tidak ada !");
System.exit(1);
} catch (IOException exp){
System.out.println("Coba Lagi");
System.exit(1);
}
}
}
-----------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class mergesort {
public String [] mergesort (String []list){
String []sorted = new String [list.length];
if (list.length ==1){
sorted = list;
} else {
int mid = list.length/2;
String[] left = null;
String[] right = null;
if ((list.length % 2) == 0) {
left = new String[list.length/2];
right = new String[list.length/2];
} else { //
left = new String[list.length/2];
right = new String[(list.length/2)+1];
}
int x=0;
int y=0;
for ( ; x < mid; x++) {
left[x] = list[x];
}
for ( ; x < list.length; x++) {
right[y++] = list[x];
}
left = mergesort(left);
right = mergesort(right);
//sorted = merge(left,right);
sorted = mergeArray(left,right);
}
return sorted;
}
private String[] merge(String[] left, String[] right) {
List<String> merged = new ArrayList<String>();
List<String> lstLeft = new ArrayList<String>(Arrays.asList(left));
List<String> lstRight = new ArrayList<String>(Arrays.asList(right));
int comp = 0;
while (!lstLeft.isEmpty() || !lstRight.isEmpty()) {
if (lstLeft.isEmpty()) {
merged.add(lstRight.remove(0));
} else if (lstRight.isEmpty()) {
merged.add(lstLeft.remove(0));
} else { // they both have elements, compare them
comp = lstLeft.get(0).compareTo(lstRight.get(0));
if (comp > 0) {
merged.add(lstRight.remove(0));
} else if (comp < 0) {
merged.add(lstLeft.remove(0));
} else { // they're equal -- just put one in
merged.add(lstLeft.remove(0));
}
}
}
return merged.toArray(new String[merged.size()]);
}
private String[] mergeArray(String[] left, String[] right) {
String[] merged = new String[left.length+right.length];
int lIndex = 0;
int rIndex = 0;
int mIndex = 0;
int comp = 0;
while (lIndex < left.length || rIndex < right.length) {
if (lIndex == left.length) {
merged[mIndex++] = right[rIndex++];
} else if (rIndex == right.length) {
merged[mIndex++] = left[lIndex++];
} else { // they both have elements. Compare them
comp = left[lIndex].compareTo(right[rIndex]);
if (comp > 0) {
merged[mIndex++] = right[rIndex++];
} else if (comp < 0) {
merged[mIndex++] = left[lIndex++];
} else { // they're equal
merged[mIndex++] = left[lIndex++];
}
}
}
return merged;
}
public static String[] readFile(String filename) throws IOException {
BufferedReader inputStream = new BufferedReader(new FileReader(filename));
List<String> testList = new ArrayList<String>();
String[] sample = null;
String l;
while ((l = inputStream.readLine()) != null) {
sample = l.split(" ");
for (int x=0; x < sample.length; x++) {
testList.add(sample[x]);
}
}
inputStream.close();
return testList.toArray(new String[testList.size()]);
}
}
-------------------------------------------------------------------------------------------------------
LEGALLY LEGACY of INFORMATION SYSTEM
No comments:
Post a Comment