Kumpulan Contoh Source Code dari Sorting dan Searching di Java

Pada Kesempatan kali ini, saya akan membagikan Kumpulan Contoh Source Code dari Sorting dan Searching menggunakan bahasa pemrograman  Java. kalian bisa mencoba sesuai kebutuhan kalian. langsung saja kalian pahami source code di bawah ini:

  1. Contoh Source Code Array Search
  2. import java.util.Vector;

    public class ArraySearch {

    int hasil;
    boolean ketemu = false;
    private Vector<Integer> vectorArs;

    public boolean Search(int cari) {
    new Data();
    int batas = Data.getUkuranVector();
    vectorArs = new Vector<Integer>(batas);
    vectorArs.addAll(Data.getVector());
    int[] ArySearch = new int[batas];
    for (int i = 0; i < (batas - 1); i++) {
    ArySearch[i] = vectorArs.get(i);
    }
    int index = 0;
    int value;
    while ((!ketemu)  && (index < batas)) {
    value = ArySearch[index];
    if (cari == value) {
    ketemu = true;
    hasil = index;
    break;
    }
    index++;
    }
    return ketemu;
    }

    public int getHasil() {
    return hasil + 1;
    }

    }

  3. Contoh Source Code Binary 
  4. import java.util.Vector;

    public class Binary {

    int hasil;
    boolean ketemu = false;
    private Vector<Integer> vectorBnr;

    public boolean Search(int cari) {
    new Data();
    int ukuran = Data.getUkuranVector();
    vectorBnr = new Vector<Integer>(ukuran);
    vectorBnr.addAll(Data.getVector());
    int[] ArrayBinary = new int[ukuran];
    for (int i = 0; i < (ukuran -1); i++) {
    ArrayBinary[i] = vectorBnr.get(i);
    }
    int low = 0;
    int high = Data.getUkuranVector()-1;
    int mid = (low + high) / 2;
    int midValue;
    while (low < high) {
    midValue = ArrayBinary[mid];
    if (midValue == cari) {
    ketemu = true;
    hasil = mid;
    break;
    } else {
    if (cari > midValue)
    low = mid + 1;
    else
    high = mid;
    }
    mid = (low + high) / 2;
    }
    return ketemu;
    }

    public int getHasil() {
    return hasil + 1;
    }
    }
  5. Contoh Source Code Bubble
  6. import java.util.Vector;

    public class Bubble {
    static Vector<Integer> vectorBubble;

    public static Vector<Integer> Sort(Vector<Integer> vectorBubble) {
    int n = 0;
    int ukuran = Data.getUkuranVector();
    while (n < ukuran) {
    for (int i = 1; i < ukuran; i++) {
    if (vectorBubble.get(i - 1) > vectorBubble.get(i)) {
    int temp = vectorBubble.get(i);
    vectorBubble.set(i, vectorBubble.get(i - 1));
    vectorBubble.set(i - 1, temp);
    }
    }
    n++;
    }
    return vectorBubble;
    }
    }
  7. Contoh Source Code Insertion
  8. import java.util.Vector;

    public class Insertion {
    public static Vector<Integer> Sort(Vector<Integer> vectorInsertion) {
    int i=1;
    int index;
    int ukuran = Data.getUkuranVector();
    while (i<ukuran){
    int temp=vectorInsertion.get(i);
    for(index=i; index>0; index--){
    if (temp < vectorInsertion.get(index-1)){
    vectorInsertion.set(index, vectorInsertion.get(index-1));
    }
    else break;
    }
    vectorInsertion.set(index, temp);
    i++;
    }
    return vectorInsertion;
    }
    }
  9. Contoh Source Code Quick
  10. import java.util.Vector;

    public class Quick {

    private static void swap(Vector<Integer> vectorQuick, int left, int right) {
    int temp = vectorQuick.get(left);
    vectorQuick.set(left, vectorQuick.get(right));
    vectorQuick.set(right, temp);
    }

    public static Vector<Integer> quickSort(Vector<Integer> vectorQuick) {
    int ukuran = Data.getUkuranVector() - 1;
    quickSortRecursive(vectorQuick, 0, ukuran);
    return vectorQuick;
    }

    private static void quickSortRecursive(Vector<Integer> vectorQuick,
    int left, int right) {
    int pivot;
    if (left >= right)
    return;
    pivot = partition(vectorQuick, left, right);
    quickSortRecursive(vectorQuick, left, pivot - 1);
    quickSortRecursive(vectorQuick, pivot + 1, right);
    }

    public static int partition(Vector<Integer> vectorQuick, int left, int right) {
    while (true) {
    while ((left < right)
    && (vectorQuick.get(left) < vectorQuick.get(right)))
    right--;
    if (left < right)
    swap(vectorQuick, left, right);
    else
    return left;
    while ((left < right)
    && (vectorQuick.get(left) < vectorQuick.get(right)))
    left++;
    if (left < right)
    swap(vectorQuick, left, right--);
    else
    return right;
    }
    }
    }
  11. Contoh Source Code Selection
  12. import java.util.Vector;

    public class Selection {
    public static Vector<Integer> Sort(Vector<Integer> vectorSelection) {
    int i;
    int ukuran = Data.getUkuranVector();
    int max;
    while (ukuran > 0){
    max = 0;
    for(i=1; i<ukuran; i++){
    if (vectorSelection.get(max) < vectorSelection.get(i)) {
    max = i;
    }
    }
    int temp = vectorSelection.get(max);
    vectorSelection.set(max, vectorSelection.get(ukuran-1));
    vectorSelection.set(ukuran-1, temp);
    ukuran--;
    }
    return vectorSelection;
    }
    }
  13. Contoh Source Code vectorShell
  14. import java.util.Vector;

    public class Shell {

    static int N;
    static int distance;
    static int j;
    static int i;
    static Vector<Integer> vectorShell;

    public static Vector<Integer> shellSort(Vector<Integer> vectorShell) {
    N = Data.getUkuranVector();
    distance = N / 2;
    while (distance > 0) {
    for (i = 0; i < N - distance; i++) {
    j = i + distance;
    if (vectorShell.get(i) > vectorShell.get(j)) {
    int temp = vectorShell.get(i);
    vectorShell.set(i, vectorShell.get(j));
    vectorShell.set(j, temp);
    }
    }
    distance = distance / 2;
    }
    return vectorShell;
    }
    }
  15. Contoh Source Code 
  16. import java.util.Vector;

    public class VectorSearch {
    int hasil;
    boolean ketemu = false;
    private Vector<Integer> vectorCv;

    public boolean Search(Integer cari) {
    new Data();
    vectorCv = new Vector<Integer>(Data.getUkuranVector());
    vectorCv.addAll(Data.getVector());
    if (vectorCv.contains(cari)) {
    hasil = vectorCv.indexOf(cari) + 1;
    }
    return ketemu = true;
    }
    public int getHasil() {
    return hasil;
    }
    }
Selamat Mencoba! selamat pusing dengan coding. sekian dulu artikel Kumpulan Contoh Source Code dari Sorting dan Searching di Java ini. semoga tutorial ini dapat bermanfaat untuk kalian semua. Terima kasih.

0 Response to "Kumpulan Contoh Source Code dari Sorting dan Searching di Java"

Post a Comment