Sorting adalah sebuah proses merangkai benda dalam urutan tertentu dan/atau dalam himpunan yang berbeda, dan oleh karena itu dia memiliki dua arti umum yang berbeda:
- pengurutan: merangkai benda yang sejenis, sekelas, dll, dalam urutan yang teratur,
- kategorisasi: pengelompokan dan pemberian label kepada benda dengan sifat yang serupa.
1. bubble sort
public class Bubble_Sort {
public static int[] A = {2,4,0,5,7,9,1,6,8,3};
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
Bubble();
System.out.println("Sesudah di urutkan :");
printA();
}
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void Bubble() {
for(int i = 1; i < 10; i++) {
for(int j = i; j < 10; j++) {
if(A[i - 1] > A[j]) {
int dummy = A[i - 1];
A[i - 1] = A[j];
A[j] = dummy;
}
}
}
}
}
public static int[] A = {2,4,0,5,7,9,1,6,8,3};
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
Bubble();
System.out.println("Sesudah di urutkan :");
printA();
}
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void Bubble() {
for(int i = 1; i < 10; i++) {
for(int j = i; j < 10; j++) {
if(A[i - 1] > A[j]) {
int dummy = A[i - 1];
A[i - 1] = A[j];
A[j] = dummy;
}
}
}
}
}
2. selection sort
public class Selection_Sort {
public static int[] A = {2,4,0,5,7,9,1,6,8,3};
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void selection() {
for (int y = 0; y < 10; y++) {
int min = y;
for (int x = y; x < 10; x++) {
if (A[x] < A[min]) {min = x; }
}
int temp = A[y];
A[y] = A[min];
A[min] = temp;
}
}
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
selection();
System.out.println("Sesudah di urutkan :");
printA();
}
}
public static int[] A = {2,4,0,5,7,9,1,6,8,3};
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void selection() {
for (int y = 0; y < 10; y++) {
int min = y;
for (int x = y; x < 10; x++) {
if (A[x] < A[min]) {min = x; }
}
int temp = A[y];
A[y] = A[min];
A[min] = temp;
}
}
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
selection();
System.out.println("Sesudah di urutkan :");
printA();
}
}
3. insertion sort
public class Insertion_Sort {
public static int[] A = { 2,4,0,5,7,9,1,6,8,3};
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
insertion();
System.out.println("Sesudah di urutkan :");
printA();
}
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void insertion() {
for (int i = 1; i < 10; i++){
int j = i;
int B = A[i];
while ((j > 0) && (A[j-1] > B)){
A[j] = A[j-1];
j--;
}
A[j] = B;
}
}
}
public static int[] A = { 2,4,0,5,7,9,1,6,8,3};
public static void main(String[] args) {
System.out.println("Sebelum di urutkan :");
printA();
insertion();
System.out.println("Sesudah di urutkan :");
printA();
}
public static void printA() {
for(int i = 0; i < 10; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void insertion() {
for (int i = 1; i < 10; i++){
int j = i;
int B = A[i];
while ((j > 0) && (A[j-1] > B)){
A[j] = A[j-1];
j--;
}
A[j] = B;
}
}
}
No comments:
Post a Comment