Rabu, 12 Juni 2013

Shell Sort

#include <iostream>
#include <cstdio>
using namespace std;
void cetak(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;i++)
        cout<<array[i] << " " ;
    cout << endl;
}

void shellSort(int *array,int length) {
     int i, temp, flag = 1  ;
     int d = length;
     while( flag || (d > 1))  // boolean flag (true jika <> 0 )
     {
          flag = 0;        // reset flag ke 0
          d = (d+1) / 2;  // d = gap
          for (i = 0; i < (length-d); i++)
        {
               if (array[i + d] < array[i])
              {
                      temp = array[i + d];      // swap
                      array[i + d] = array[i];
                      array[i] = temp;
                      flag = 1;       // swap telah terjadi
              }
         }
     }
}


int main()
{

    int a[]={9,6,5,23,2,66,14,8,2,7,1,8};   // array to sort
    cetak(a,12);               // print elements
    shellSort(a,12);                 //call to bubble sort 
    cetak(a,12);               // print elements
    return 0;
}

Select Sort

#include <iostream>
#include <cstdio>
using namespace std;
void cetak(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;i++)
        cout<<array[i] << " " ;
    cout << endl;
}

void selectionSort(int *arr,int n)
{
    int i, j, minIndex, tmp;
    for (i = 0; i < n - 1; i++)
    {
        minIndex = i;
        for (j = i + 1; j < n; j++) //cari elemen terkecil dari array
            if (arr[j] < arr[minIndex]) minIndex = j;
       
        if (minIndex != i)
        {
            tmp = arr[i];
            arr[i] = arr[minIndex]; // pindah ke akhir array
            arr[minIndex] = tmp;
        }
    }
}

int main()
{

    int a[]={9,6,5,23,2,66,14,8,2,7,1,8};   // array to sort
    cetak(a,12);               // print elements
    selectionSort(a,12);                 //call to select sort 
    cetak(a,12);               // print elements
    return 0;
}

Quick Sort

#include <iostream>
#include <cstdio>
using namespace std;
void cetak(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;i++)
        cout<<array[i] << " " ;
    cout << endl;
}




//Function to determine the partisis
// partisis the array and returns the middle subscript
int partisi(int *array, int top, int bottom)
{
     int x = array[top];
     int i = top - 1;
     int j = bottom + 1;
     int temp;
     do
     {
           do     
           {
                  j--;
           }while (x >array[j]);

          do 
         {
                 i++;
          } while (x <array[i]);

          if (i < j)
         {
                 temp = array[i];   
                 array[i] = array[j];
                 array[j] = temp;
         }
     }while (i < j);    
     return j;           // returns middle subscript 
}


void quickSort(int *num, int top, int bottom)
{
      // top = subscript of beginning of array
      // bottom = subscript of end of array
     
     int middle;
     if (top < bottom)
    {
          middle = partisi(num, top, bottom);
          quickSort(num, top, middle);   // sort first section
          quickSort(num, middle+1, bottom);    // sort second section
     }
     return;
}


int main()
{
    int a[]={9,6,5,23,2,66,14,8,2,7,1,8};   // array to sort
    cetak(a,12);               // print elements
    quickSort(a,0,12);                 //call to bubble sort 
    cetak(a,12);               // print elements
    return 0;
}

Sorting di C++ dengan Insert Sort

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

void cetak(vector<int> ve)
{
    int i=0;
    for(i = 0;i < ve.size();i++)
        cout << ve[i] << " ";
    cout << endl;
}

void insertionSort(vector<int> &ve)
{
    int i, j, minIndex, tmp;
    for ( i = 1; i < ve.size(); i++)
    {
        tmp = ve[i];
        j = i;
        while ((j > 0) && (tmp < ve[j-i] ))
        {
            ve[j] = ve[j-i];
            j--;
        }
        ve[j] = tmp;
    }
}


int main()
{
    vector<int> vv;
    vv.push_back(9);
    vv.push_back(2);
    vv.push_back(5);
    //Svv.push_back(1);
    cetak(vv);
    insertionSort(vv);//
    cetak(vv);
    return 0;
   
}

Sorting di C++

// Metode Bublesort
#include <iostream>
#include <cstdio>
using namespace std;
void cetak(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<length;i++)
        cout<<array[i] << " " ;
    cout << endl;
}

void bubbleSort(int *array,int length)//Bubble sort function
{
    int i,j;
    for(i=0;i<length;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i] < array[j])
            {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;
            }

        }
    }
}

int main()
{

    int a[]={9,6,5,23,2,66,14,8,2,7,1,8};   // array to sort
    cetak(a,12);               // print elements
    bubbleSort(a,12);                 //call to bubble sort 
    cetak(a,12);               // print elements
    return 0;
}