[STRUKTUR_DATA]

1.     INSERTION SORT
a.      Script :
#include <conio.h>
using namespace std;
int main()
{
    int a[10], i, j, k, temp;
    cout<<"masukan angka:\n";
    for (i = 0; i < 16; i++)
    {
        cin>>a[i];
    }
    for (i = 1; i < 10; i++)
    {
        for (j = i; j >= 1; j--)
        {
            if (a[j] < a[j-1])
            {
                temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
            else
                break;
        }
    }
    cout<<"sorted array\n"<<endl;
    for (k = 0; k < 16; k++)
    {
          cout<<a[k]<<endl;
    }
    getch();
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ_7jQQncH19W7zzNeSIW74sM8y6-vRJSFtPVMahpaCOZj65ZKh9H_Y7P0-McNzjB3N8U_TW5jZdcldiybbV-Np-EesU_-z3dWy9_dhyphenhyphenZQ4Z0ND-RdJfZtmekrdpUcNfQs6oWWc8T9y20/s1600/1.png



2.     TREE SORT
a.      Script :
#include<iostream>
using namespace std;
struct tree{
    int info;
    tree *Left, *Right;
};
tree *root;
class TreeSort{
    public:
        int no_of_elements;
        int elements[10];
    public:
        void getarray();
        void sortit();
        void insert1(int);
        tree *insert2(tree *, tree *);
        void display(tree *);
};
void TreeSort::getarray(){
    cout<<"Berapa banyak elmen yang anda inginkan? ";
    cin>>no_of_elements;
    cout<<"masukan angka: ";
    for(int i=0;i<no_of_elements;i++){
        cin>>elements[i];
    }
}
void TreeSort::sortit(){
    for(int i = 0; i  < no_of_elements; i++){
        insert1(elements[i]);
    }
}
tree* TreeSort::insert2(tree *temp,tree *newnode){
    if(temp==NULL){
        temp=newnode;
    }
    else if(temp->info < newnode->info){
        insert2(temp->Right,newnode);
        if(temp->Right==NULL)
            temp->Right=newnode;
    }
    else{
        insert2(temp->Left,newnode);
        if(temp->Left==NULL)
            temp->Left=newnode;
    }
    return temp;
}
void TreeSort::insert1(int n){
    tree *temp=root,*newnode;
    newnode=new tree;
    newnode->Left=NULL;
    newnode->Right=NULL;
    newnode->info=n;
    root=insert2(temp,newnode);
}
/* Inorder traversal */
void TreeSort::display(tree *t = root){
    if(root==NULL){
        cout<<"Nothing to display";
    }else
    if(t!=NULL){
        display(t->Left);
        cout<<t->info<<" ";
        display(t->Right);
    }
}
int main(){
    TreeSort TS;
    TS.getarray();
    TS.sortit();
    TS.display();
    return 0;
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrLilLEmi8VhQgiFhUhK8wxGxdSDoKX__eLXo4zhtozfD80fYWR_XYehjKZ0rj9JChrIA8OLoUc-lWEOgvY1gVHb23Qhor4KQ4mI1m-1KJRX1tGXV-6jpHcVWpPrBBaXQ-y1lNcAQe49k/s1600/2.png



3.     BUBBLE SORT
a.      Script :
#include<iostream>
using namespace std;

int main(){
     //declaring array
      int array[5];
      cout<<"masukan 5 angka random : "<<endl;
      for(int i=0; i<5; i++)
      {
     //Taking input in array 
       cin>>array[i];       
      } 
      cout<<endl;
      cout<<"Input array : "<<endl;
     
      for(int j=0; j<5; j++)
      {
       //Displaying Array
       cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;        
      }  
      cout<<endl;
    // Bubble Sort Starts Here
     int temp;
     for(int i2=0; i2<=4; i2++)
   {
     for(int j=0; j<4; j++)
     {
        //Swapping element in if statement   
           if(array[j]>array[j+1])
       {
        temp=array[j];
        array[j]=array[j+1];
        array[j+1]=temp;       
       }
     }        
   }
   // Displaying Sorted array
      cout<<"  Sorted Array : "<<endl;
     for(int i3=0; i3<5; i3++)
   {
    cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl; 
   }  
return 0;
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVzVN_VdvesoBeSLg8OPItdm0M0wYiI0WqTpV5q8CTWt6dKXx5OlEAXphk-SKZVu9Wpo3aYBKjCtEI-sB_mfWzkc9Y1DcTuwfJZCuMQ9PmkDAJll76ogTJ6_vp5D7kIez1lovmekdagjc/s1600/3.png



4.     EXCHANGE SORT
a.      Script :
using namespace std;
#include<iostream>
int main(void)
{
          int array[5];           // An array of integers.
          int length = 5;                 // Lenght of the array.
          int i, j;
          int temp;

        //Some input
          for (i = 0; i < 5; i++)
          {
                   cout << "masukan angka: ";
                   cin >> array[i];
          }

          //Algorithm
          for(i = 0; i < (length -1); i++)
          {
                   for (j=(i + 1); j < length; j++)
                   {
                             if (array[i] < array[j])
                             {
                                      temp = array[i];
                                      array[i] = array[j];
                                      array[j] = temp;
                             }
                   }
          }
          //Some output
          for (i = 0; i < 5; i++)
          {
                   cout << array[i] << endl;
          }
}

b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8UYv1BDp0PF4MyezjZgzeRqhZxTUJNYVQHEaUafg_W5tJCiimqcbxrQpyqp29MJkl28Isl1xlldH7Q-m9l64E8e-xGAKrny3Pw-aSPv04HU42_5QH6kOzzT85EXlkrsRU4dAi7BFgBrs/s1600/4.png



5.     SELECTION SORT
a.      Script :
#include<iostream>
#include<conio.h>

using namespace std;


template <class T>
void s_sort(T a[],int n)
{
     int i,j,t;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
                                       {
                                                    t=a[i];
                                                    a[i]=a[j];
                                                    a[j]=t;
                                       }
                     }
     }
}


int main()
{
    int a[100],i,n;
    cout<<"Masukan jumlah elmen:\n";
    cin>>n;
    cout<<"\nmasukan angka:\n";
    for(i=0;i<n;i++)
    {

                      cout<<"\nmasukan:";
                    cin>>a[i];
    }
    s_sort(a,n);
    cout<<"\nAfter Sorting\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\t";
    }
    getch();
    return 0;
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2-w4wDg8sXcJQyBxnzvf6V6TKbaMlfccTTxBGKzpZZslGhzqCrQkvWvIAWeWOJzBnrvaRjuQVkE7tKNpyE69yjrQBliQA-TU6V1-hat3Iq6xtVU8SfZHRrmS60_caF9creCdkTmO81NE/s1600/5.png



6.     HEAP SORT
a.      Script :
#include <iostream>
#include <conio.h>
using namespace std;
void max_heapify(int *a, int i, int n)
{
    int j, temp;
    temp = a[i];
    j = 2*i;
    while (j <= n)
    {
        if (j < n && a[j+1] > a[j])
            j = j+1;
        if (temp > a[j])
            break;
        else if (temp <= a[j])
        {
            a[j/2] = a[j];
            j = 2*j;
        }
    }
    a[j/2] = temp;
    return;
}
void heapsort(int *a, int n)
{
    int i, temp;
    for (i = n; i >= 2; i--)
    {
        temp = a[i];
        a[i] = a[1];
        a[1] = temp;
        max_heapify(a, 1, i - 1);
    }
}
void build_maxheap(int *a, int n)
{
    int i;
    for(i = n/2; i >= 1; i--)
    {
        max_heapify(a, i, n);
    }
}
int main()
{
    int n, i, x;
    cout<<"Masukan jumlah elmen:\n";
    cin>>n;
    int a[20];
    for (i = 1; i <= n; i++)
    {
        cout<<"masukan angka:"<<(i)<<endl;
        cin>>a[i];
    }
    build_maxheap(a,n);
    heapsort(a, n);
    cout<<"sorted output\n";
    for (i = 1; i <= n; i++)
    {
        cout<<a[i]<<endl;
    }
    getch();
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqD4JbSpK43ZA6iDXmIpoXYyDBU99BpX01NEUgpPrF_2qkwA3l01ILorEAcxYFz3KU688r-KweBYx47ohxf__-7YBAP9l8HY4Yjd-mdrSvKEkc5XH_OEeFt3N4Ep6uiAr0RTrHyCbGrHQ/s1600/6.png



7.     QUICK SORT
a.      Script :
#include <iostream>

void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
void swapNoTemp(int& a, int& b);
void print(int array[], const int& N);

using namespace std;

int main()
{
    int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
    int N = sizeof(test)/sizeof(int);

    cout << "Size of test array :"  << N << endl;

    cout << "Before sorting : " << endl;
    print(test, N);

    quickSort(test, 0, N-1);

    cout << endl << endl << "After sorting : " << endl;
    print(test, N);
    
    return 0;
}

/**
 * Quicksort.
 * @param a - The array to be sorted.
 * @param first - The start of the sequence to be sorted.
 * @param last - The end of the sequence to be sorted.
*/
void quickSort( int a[], int first, int last )
{
    int pivotElement;

    if(first < last)
    {
        pivotElement = pivot(a, first, last);
        quickSort(a, first, pivotElement-1);
        quickSort(a, pivotElement+1, last);
    }
}

/**
 * Find and return the index of pivot element.
 * @param a - The array.
 * @param first - The start of the sequence.
 * @param last - The end of the sequence.
 * @return - the pivot element
*/
int pivot(int a[], int first, int last)
{
    int  p = first;
    int pivotElement = a[first];

    for(int i = first+1 ; i <= last ; i++)
    {
        /* If you want to sort the list in the other order, change "<=" to ">" */
        if(a[i] <= pivotElement)
        {
            p++;
            swap(a[i], a[p]);
        }
    }

    swap(a[p], a[first]);

    return p;
}


/**
 * Swap the parameters.
 * @param a - The first parameter.
 * @param b - The second parameter.
*/
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

/**
 * Swap the parameters without a temp variable.
 * Warning! Prone to overflow/underflow.
 * @param a - The first parameter.
 * @param b - The second parameter.
*/
void swapNoTemp(int& a, int& b)
{
    a -= b;
    b += a;// b gets the original value of a
    a = (b - a);// a gets the original value of b
}

/**
 * Print an array.
 * @param a - The array.
 * @param N - The size of the array.
*/
void print(int a[], const int& N)
{
    for(int i = 0 ; i < N ; i++)
        cout << "array[" << i << "] = " << a[i] << endl;
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEha93LlD5VCQho_dRmA13pkwIlkxEfgyzVZFize_9OGc4RTG10vMp11FmK-uajD_pa_lJQXIxxdNADRLl1ng4cjZDVdOfkRcQYe0fJ0yI2Vnnfobj8Tj05wySWSPqyYjagIvm52Gn-4N1E/s1600/7.png



8.     MERGE SORT
a.      Script :
using namespace std;
#include <iostream>
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
    int mid;
    if (low < high)
    {
        mid=(low+high)/2;
        mergesort(a,low,mid);
        mergesort(a,mid+1,high);
        merge(a,low,high,mid);
    }
    return;
}
void merge(int *a, int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    k = low;
    j = mid + 1;
    while (i <= mid && j <= high)
    {
        if (a[i] < a[j])
        {
            c[k] = a[i];
            k++;
            i++;
        }
        else
        {
            c[k] = a[j];
            k++;
            j++;
        }
    }
    while (i <= mid)
    {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high)
    {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++)
    {
        a[i] = c[i];
    }
}
int main()
{
    int a[20], i, b[20];
    cout<<"enter  the elements\n";
    for (i = 0; i < 5; i++)
    {
        cin>>a[i];
    }
    mergesort(a, 0, 4);
    cout<<"sorted array\n";
    for (i = 0; i < 5; i++)
    {
        cout<<a[i];
    }
    cout<<"\nenter  the elements\n";
    for (i = 0; i < 5; i++)
    {
        cin>>b[i];
    }
    mergesort(b, 0, 4);
    cout<<"sorted array\n";
    for (i = 0; i < 5; i++)
    {
        cout<<b[i];
    }
    getch();
}
b.     Gambar :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyF6vAOtDDtDwQ-Z9N-QOxAK_Bej5kOQqS_Od5I0MpFoi_578EKhVd2U27SGetKzUacQZPIFEAjkB9Z0qMYQddiGB4QHJCXgyxzW8rbDKJM8Ei0yQ-QibhcJ_7AyyJu_cBoEoxybMlk7k/s1600/8.png



9.     SHELLSORT
a.      Srcipt :
using namespace std;
#include <iostream>
#include <conio.h>
void read(int a[10], int n){
          cout<<"Reading\n";
          for(int i = 0; i < n; i++)
                   cin>>a[i];
}
void display(int a[10], int n){
          for(int i = 0; i < n; i++)
                   cout<<a[i]<<"\t";
}
void shell(int a[10], int n){
          int gap = n/2;
          do{
                   int swap;
                   do{
                             swap = 0;
                             for(int i = 0; i < n - gap; i++)
                                      if(a[i] > a[i+gap])
                                      {
                                                int t = a[i];
                                                a[i] = a[i+gap];
                                                a[i+gap] = t;
                                                swap = 1;
                                      }
                   }
                   while(swap);
          }
          while(gap = gap / 2);
}
int main(){
          int a[10];
          int n;
          cout<<"enter n\n";
          cin>>n;
          read(a,n);
          cout<<"before sorting\n";
          display(a,n);
          shell(a,n);
          cout<<"\nafter sorting\n";
          display(a,n);
          getch();
}
b.     Gambar :

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXtjEn9qcKrQkymS3yDmD8dDuJiEmFq0oCkLE3s1RMDNfJhkrS1bebBZA2xoIEMrSN6zup94OYMw-qIOjDE_U95vQidhBq1c21dXeq57VAs_HoBsIN9zcinLRPhyphenhyphenCeKEENQN6_enzDyrE/s1600/9.png

Previous
Next Post »