1 / 12

SORTING

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++) {

kynan
Download Presentation

SORTING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SORTING Ahmad Maulana Ahmad Syaogi Ikhsan Permana

  2. 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]); } }

  3. Contoh Insertion Sort

  4. 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;         }     }

  5. 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]); } }

  6. Contoh Selection Sort

  7. 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;       } }

  8. 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); } }

  9. 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); } }

  10. 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

  11. 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

  12. TERIMA KASIH

More Related