Selasa, 18 Desember 2018

Sorting and Searching


-Sorting type:
Ascending
Descending
-Simple sort:
  Bubble sort
  Selection sort
  Insertion sort
-Intermediate sort: 
  Merge Sort
           –  Quick Sort


  Contoh bubble sort:
void Bubble(int *DataArr, int n)
{
    int i, j;
    for(i=1; i<n; i++)
    for(j=n-1; j>=i; j--)
    if(DataArr[j-1] > DataArr[j])
               Swap(&DataArr[j-1],&DataArr[j]);

}
Contoh selection sort :
for(i=0; i<N-1; i++){      /* N=number of data */
  Set idx_smallest equal to i
  for(j=i+1; j<N; j++){
  If array[ j ] < array [ idx_smallest ] then idx_smallest = j
    }
  Swap array[ i ] with array[ idx_smallest ]
}
Contoh insertion sort :
for(i=1; i<n; i++) {
     x = A[i], insert x to its suitable place between A[0] and A[i-1].
}
MERGE SORT 
Merge Sort is a sorting algorithm based on the divide-and-conquer algorithm
Divide-and-conquer is a general algorithm design paradigm
Divide: divide the input data in two disjoint subsets
Recur: solve the sub problems associated with subsets
Conquer: combine the solutions for each subset into a solution
SEARCHING 
Searching is an action to retrieve information based on particular key from some saved information
Key is used to do record searching which is desired from a set of data list
Key must be unique, means there must not be any same key in the data
Example:
  Student data consists of name, nim, gender, address, place and date of birth
  nim is used as key from the data, as it is unique.
 
Contoh quick sort :
void QuickSort(int left, int right)
{
      if(left < right){
            //arrange elements  R[left],...,R[right] that
            //producing new sequence:
            R[left],...,R[A-1] < R[A] and R[A+1],...,R[right] > R[A].
            QuickSort(left, A-1);
            QuickSort(A+1, right);
       }
}

Tidak ada komentar:

Posting Komentar