Pada Kesempatan kali ini, aku akan membagikan Kumpulan Contoh Source Code dari Sorting dan Searching memakai bahasa pemrograman Java. kalian sanggup mencoba sesuai kebutuhan kalian. eksklusif saja kalian pahami source code di bawah ini:
- Contoh Source Code Array Search
- Contoh Source Code Binary
- Contoh Source Code Bubble
- Contoh Source Code Insertion
- Contoh Source Code Quick
- Contoh Source Code Selection
- Contoh Source Code vectorShell
- Contoh Source Code
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
0 comments:
Post a Comment