Thursday, November 28, 2013

Menara Hanoi

Berikut adalah algoritma membuat menara hanoi tidak menggunkan fungsi rekursif:

class Stack:

package hanoi;

public class Stack {

    private String name;
    private int[] data;
    private int size;
    private int top;

    public Stack() {
    }

    public Stack(String name, int[] data, int size, int top) {
        this.name = name;
        this.data = data;
        this.size = size;
        this.top = top;
    }

    public int[] getData() {
        return data;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public void push(int item) {
        if (top == size) {
           
        } else {
            data[top] = item;
            System.out.println("Push " + item + " to " + name);
            top++;
        }
    }

    public void pop() {
        if (top == 0) {
           
        } else {
            System.out.println("Pop " + data[top - 1] + " from " + name);
            top--;
        }
    }

    public int getTopStack() {
        int topStack = 0;
        if (top != 0) {
            topStack = data[top - 1];
        }
        return topStack;
    }
}

class Main

package hanoi;

public class Main {

    static void printData(Stack s) {
        System.out.print(s.getName() + ": ");
        for (int x = 0; x < s.getTop(); x++) {
            System.out.print(s.getData()[x] + " ");
        }
        System.out.println("");
    }

    static void move(Stack from, Stack to) {
        int willMove = from.getTopStack();
        from.pop();
        to.push(willMove);
    }

    static void leftToRight(Stack from, Stack to, Stack temp) {
        System.out.println("");
        while (from.getTop() != 0) {
            if (from.getTopStack() > temp.getTopStack() && temp.getTop() == 1) {
                break;
            } else {
                if (to.getTop() == 0 || to.getTopStack() > from.getTopStack()) {
                    move(from, to);
                } else {
                    move(from, temp);
                    rightToLeft(from, to, temp);
                }
            }
        }
        if (temp.getTop() > 0) {
            move(temp, from);
        }
    }

    static void rightToLeft(Stack from, Stack to, Stack temp) {
        System.out.println("");
        while (to.getTop() != 0) {
            if (to.getTopStack() > temp.getTopStack() && temp.getTop() == 1) {
                break;
            } else {
                if (from.getTop() == 0 || from.getTopStack() > to.getTopStack()) {
                    move(to, from);
                } else {
                    move(to, temp);
                    leftToRight(from, to, temp);
                }
            }
        }
        if (temp.getTop() > 0) {
            move(temp, to);
        }
    }

    static void hanoi(Stack from, Stack to, Stack temp) {
        while (to.getTop() != to.getSize()) {
            leftToRight(from, to, temp);
            if (from.getSize() == 0 && temp.getSize() == 0) {
                break;
            }
            System.out.println("==============================");
            printData(from);
            printData(temp);
            printData(to);
            System.out.println("==============================");
        }
    }

    public static void main(String[] args) {
        int size = 4;
        Stack s1 = new Stack("Tower 1", new int[size], size, 0);
        Stack s2 = new Stack("Tower 2", new int[size], size, 0);
        Stack s3 = new Stack("Tower 3", new int[size], size, 0);
        s1.push(4);
        s1.push(3);
        s1.push(2);
        s1.push(1);
        hanoi(s1, s3, s2);
    }
}

Monday, November 18, 2013

Merge Sort In C

#include <stdio.h>

int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};

void swap(int first, int second) {
    int tampung = bil[second];
    int i;
    for (i = second - 1; i >= first; i--) {
        bil[i + 1] = bil[i];
    }
    bil[first] = tampung;
}

void mergeSort(int startBawah, int endAtas) {
    if (startBawah >= endAtas) {
        return;
    }
    int middle = (startBawah + endAtas) / 2;
    int endBawah = middle;
    int startAtas = middle + 1;
    mergeSort(startBawah, endBawah);
    mergeSort(startAtas, endAtas);
    while ((startBawah <= endBawah) && (startAtas <= endAtas)) {
        if (bil[startBawah] > bil[startAtas]) {
            swap(startBawah, startAtas);
            startBawah++;
            startAtas++;
            endBawah++;
        } else {
            startBawah++;
        }
    }
}

int main() {
    mergeSort(0, n - 1);
    int x;
    for (x = 0; x < n; x++) {
        printf("%d ", bil[x]);
    }
    return 0;
}


Quick Sort In C

#include <stdio.h>

int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};

void swap(int first, int second) {
    int tampung = bil[first];
    bil[first] = bil[second];
    bil[second] = tampung;
}

void quickSort(int low, int high) {
    int x = low, y = high;
    int pivot = bil[low + (high - low) / 2];
    while (x <= y) {
        while (bil[x] < pivot) {
            x++;
        }
        while (bil[y] > pivot) {
            y--;
        }
        if (x <= y) {
            swap(x, y);
            x++;
            y--;
        }
    }
    if (low < y) {
        quickSort(low, y);
    }
    if (x < high) {
        quickSort(x, high);
    }
}

int main() {
    quickSort(0, n - 1);
    int x;
    for (x = 0; x < n; x++) {
        printf("%d ", bil[x]);
    }
    return 0;
}

Insertion Sort In C

#include <stdio.h>

int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};

void swap(int first, int second) {
    int tampung = bil[first];
    bil[first] = bil[second];
    bil[second] = tampung;
}

void insertionSort() {
    int x, y, stop, tampung;
    for (x = 1; x < n; x++) {
        stop = x;
        for (y = (x - 1); y >= 0; y--) {
            if (bil[x] < bil[y]) {
                stop = y;
            }
        }
        tampung = bil[x];
        for (y = x - 1; y >= stop; y--) {
            swap(y, (y + 1));
        }
        bil[stop] = tampung;
    }
}

int main() {
    insertionSort();
    int x;
    for (x = 0; x < n; x++) {
        printf("%d ", bil[x]);
    }
    return 0;
}


Selection Sort In C

#include <stdio.h>

int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};

void swap(int first, int second) {
    int tampung = bil[first];
    bil[first] = bil[second];
    bil[second] = tampung;
}

void selectionSort() {
    int x, y, min;
    for (x = 0; x < n; x++) {
        min = x;
        for (y = (x + 1); y < n; y++) {
            if (bil[min] > bil[y]) {
                min = y;
            }
        }
        swap(x, min);
    }
}

int main() {
    selectionSort();
    int x;
    for (x = 0; x < n; x++) {
        printf("%d ", bil[x]);
    }
    return 0;
}

Bubble Sort In C

#include <stdio.h>

int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};

void swap(int first, int second) {
    int tampung = bil[first];
    bil[first] = bil[second];
    bil[second] = tampung;
}

void bubbleSort() {
    int x, y;
    for (x = 1; x < n; x++) {
        for (y = 0; y < n - x; y++) {
            if (bil[y] > bil[y + 1]) {
                swap(y, (y + 1));
            }
        }
    }
}

int main() {
    bubbleSort();
    int x;
    for (x = 0; x < n; x++) {
        printf("%d ", bil[x]);
    }
    return 0;
}

THIS HOUSE WOULD ALLOW STUDENT TO BRING MOBILE PHONE TO SCHOOL

This is some negative reason and also the effect from the motion
1.      Open Sexual Content Site On The Internet
Many people say that, students should bring handphone to school to help them search some content to support them on school activity or help them on their task. This is just wrong because they can open it at home. However, students are curious with the content from the internet. Even they dont open sexual content website thre will be some advertisement on some website which wiil direct them to porn or sexual content website. Thats why this is dangerous from the students to bring handphone to schools. Beside that, for some reason when the students are bring their phone to school they will feel free, they will open some website they want. This is posible for them to open sexual or porn website. However, children is children. They will act as they see, they will act as they hear, they will act as they feel comfortable. This is possible why there are a lot of crimes about porn which involve the students. It will destroy their future.

2.      Make The Children Play Games or Hearing Some Music
Not all the children at school are excited to the lesson they get. Sometime they will feel bored. At this time, students will try another activities that will not make them bored. In this case, if they bring mobile phone on their pockets, it will triggered they want to pull their phone on the pockets and as natural as a children when they are bored, they will play games. It will distract their concentration to the lesson. This is not good for them because the requirement to pass the school year are the minimal value of every lesson not one or some of them, but all the lessons. Not only to the children but also to the teacher. If the teacher caught them play games on the teaching activities, they will be punished by the teacher. We know that not all the teacher have good patient, sometime they are “killer” (means very grumpy) and it will caused violene at school. Sometime it will be some big problems and wil be printed on newspaper that “Mr. Theacher Kick Some Student’s Ass At School”. It will make the teacher be droped out from the school. And this is becaused of the children bring their phone to schools.

3.      Use Phone To Cheat

By bringing phone to schools, the children will use it as a media to cheat. They will use it to get some answer from their friends. For example when the final exam are coming, not only the children but also the teacher will help them to cheat. Based on my experience on my junior high school, the teacher will take some clever students and ask them to share their answer to the other students by mobile phone. Extremly this is not only happen in a school but also they do it to another school because in my case, the guard teacher when the exam was hold are the teacher from another school and they will leet us use our phone even there are several of them told as that “Boleh menyontek tapi jangan ramai dan jangan gadus”. This is crazy I think. It will make our mark for every lessons good but not real.