120 likes | 272 Views
SORTING. Ahmad Maulana Ahmad Syaogi Ikhsan Permana. Insertion Sort. A lgoritma. void insertionSort(Object array[], int startIdx, int endIdx) { for (int i = startIdx; i < endIdx; i++) { int k = i; for (int j = i + 1; j < endIdx; j++) {
E N D
SORTING Ahmad Maulana Ahmad Syaogi Ikhsan Permana
Insertion Sort Algoritma void insertionSort(Object array[], int startIdx, int endIdx) { for (int i = startIdx; i < endIdx; i++) { int k = i; for (int j = i + 1; j < endIdx; j++) { if (((Comparable) array[k]).compareTo(array[j])>0) { k = j; } } swap(array[i],array[k]); } }
Contoh Insertion Sort void insertionSort(int data[]) { int temp; int j; for (int i=1; i<data.length; i++) { temp = data[i]; j=i-1; while (temp<=data[j] && j>=0) { data[j+1]=data[j]; j--; } data[j+1]= temp; } }
Selection Sort Algoritma void selectionSort(Object array[], int startIdx, int endIdx) { int min; for (int i = startIdx; i < endIdx; i++) { min = i; for (int j = i + 1; j < endIdx; j++) { if (((Comparable)array[min]).compareTo(array[j])>0) { min = j; } } swap(array[min], array[i]); } }
Method Selection Sort public void selectionSort(int[] data ) { int i, indeksMin, jum, temp; jum = data.length; for (i = 0; i<=jum-1; i++){ //tiap iterasi dalam satu loop adalah satu langkah indeksMin = i; //cari nilai terkecil & simpan posisi dalam indeksMin for (int j = i+1; j < jum; j++) { if (data[j] < data[indeksMin]) indeksMin = j; } //tukarkan data[i] dengan data[indeksMin] temp = data[i]; data[i] = data[indeksMin]; data[indeksMin] = temp; } }
Merge Sort Algoritma void mergeSort(Object array[], int startIdx, int endIdx) { if (array.length != 1) { //Membagi rangkaian data, rightArr dan leftArr mergeSort(leftArr, startIdx, midIdx); mergeSort(rightArr, midIdx+1, endIdx); combine(leftArr, rightArr); } }
Quick Sort Algoritma void quickSort(Object array[], int leftIdx, int rightIdx) { int pivotIdx; /* Kondisi Terminasi */ if (rightIdx > leftIdx) { pivotIdx = partition(array, leftIdx, rightIdx); quickSort(array, leftIdx, pivotIdx-1); quickSort(array, pivotIdx+1, rightIdx); } }
public void QuickSort(int data[]) { QuickSortStep(data,0, data.length-1); } public void QuickSortStep(int data[], int in_first, int in_last) { int i,j; int first, last; int temp; first = in_first; last = in_last; if (first < last) { //bagian untuk mengatur pengurutan i = first + 1; j = last; while (data[i] <= data[first]) i++; while (data[j] > data[first]) j--; while (i<j) { temp=data[i]; data[i]= data[j]; data[j]=temp; while (data[i] <= data[first]) i++; while (data[j] > data[first]) j--; } temp=data[first]; data[first]=data[j]; data[j]=temp; QuickSortStep(data,first, j-1); QuickSortStep(data,j+1, last); } } Contoh Quick Sort
public class DemoBubbleSort { public static void main(String s[]) { DemoBubbleSort ss = new DemoBubbleSort (); ss.start(); } public void start() { int dataKu[] = {61, 25, 86, 11, 90, 5, 9, 50, 65, 27}; int index; // tampilkan data mula-mula System.out.println(“Data mula-mula:"); print(dataKu); bubbleSort(dataKu); // tampilkan data terurut System.out.println(“Data terurut:"); print(dataKu); } public void print( int[] data ) { int i; for (i=0; i<data.length; i++) System.out.print(data[i] + " "); System.out.println(); } public void bubbleSort( int array2[] ) { // loop untuk mengontrol jumlah iterasi for ( int iter = 1; iter < array2.length; iter++ ) { // loop untuk mengontrol jumlah perbandingan for ( int idx = 0; idx < array2.length - 1; idx++ ) { if ( array2[ idx ] > array2[ idx + 1 ] ) swap( array2, idx, idx + 1 ); } } } // tukar kedua elemen dalam array public void swap( int array3[], int pertama, int kedua ) { int temp; // variabel temporer temp = array3[pertama]; array3[pertama] = array3[kedua]; array3[kedua] = temp; } } Bubble Sort