Konsep Beberapa Jenis
Sorting Pada Pemrograman
Bubble Sort adalah salah satu algoritma untuk sorting data, atau kata lainnya mengurutkan data dari yang terbesar ke yang terkecil atau sebaliknya (Ascending atau Descending). Bubble sort (metode gelembung) adalah metode/algoritma pengurutan dengan dengan cara melakukan penukaran data dengan tepat disebelahnya secara terus menerus sampai bisa dipastikan dalam satu iterasi tertentu tidak ada lagi perubahan. Jika tidak ada perubahan berarti data sudah terurut. Disebut pengurutan gelembung karena masing-masing kunci akan dengan lambat menggelembung ke posisinya yang tepat. Berikut contoh implementasi bubble sort pada pemograman Java :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* @author Afira Rolobessy | |
* @version Bubble Sort | |
*/ | |
import java.util.Scanner; | |
public class BubbleSort | |
{ | |
static void bubbleSort(int[] arraytest){ | |
int n = arraytest.length; | |
int temp = 0; | |
for (int i=0; i<n; i++){ | |
for(int j=1; j<(n-i); j++){ | |
if (arraytest[j-1] > arraytest[j]){ | |
temp = arraytest[j-1]; | |
arraytest[j-1]=arraytest[j]; // menggeser angka yang lebih kecil ke posisi sebelumnya | |
arraytest[j] = temp; // angka yang lebih besar akan ditempatkan di sisi kanan | |
} | |
} | |
} | |
} | |
public static void main(String[] args){ | |
int arraytest[] = {23,16,3,42,75,536,61}; | |
System.out.println ("Array Before Doing Bubble Sort"); | |
for(int i=0; i<arraytest.length; i++){ | |
System.out.print(arraytest[i] + " "); | |
} | |
System.out.println(); | |
bubbleSort(arraytest); | |
System.out.println("Array After Doing Bubble Sort"); | |
for(int i=0; i<arraytest.length; i++){ | |
System.out.print(arraytest[i]+ " "); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* @author Afira Rolobessy | |
* @version Selection Sort | |
*/ | |
import java.util.*; | |
public class SelectionSort | |
{ | |
public static void selsort(int[] arr){ | |
int n=arr.length; | |
for(int i=0; i<n-1; i++){ | |
int MIN=i; | |
System.out.println("Sorting based on Number "+(i+1)); | |
for(int j=i+1; j<n; j++){ | |
System.out.println("Comparing "+ arr[MIN]+ " and " + arr[j]); | |
if(arr[j] < arr[MIN]){ | |
System.out.println(arr[MIN] + " is greater than "+ arr[j]); | |
MIN = j; | |
} | |
} | |
int temp = arr[i]; | |
arr[i] = arr[MIN]; | |
arr[MIN] = temp; | |
} | |
} | |
public static void main(String[] args){ | |
int[] arr= {15,21,6,3,19,20}; | |
System.out.println("Elements in the array before sorting: "+ | |
Arrays.toString(arr)); | |
selsort(arr); | |
System.out.println("Elements in the array after Sorting: "+ | |
Arrays.toString(arr)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* @author Afira Rolobessy | |
* @version Insertion Sort | |
*/ | |
public class InsertionSort | |
{ | |
public static void insertionSort(int arr[]){ | |
for (int j=1; j<arr.length; j++){ | |
int key = arr[j]; int i=j-1; | |
while ((i>-1) && (arr[i] > key)){ | |
arr[i+1] = arr[i]; i--; | |
} | |
arr[i+1] = key; | |
} | |
} | |
static void printArray(int arr[]){ | |
int len = arr.length; | |
for(int i=0; i<len; i++) | |
System.out.print(arr[i]+" "); | |
System.out.println(); | |
} | |
public static void main(String args[]){ | |
int[] arr1= {21,18,15,23,52,12,61}; | |
insertionSort(arr1); | |
printArray(arr1); | |
} | |
} |
Komentar
Posting Komentar