Student Of Fortune
Showing posts with label Csharp. Show all posts
Showing posts with label Csharp. Show all posts

Bubble Sort In C Language

   Bubble Sort is a sorting method with principles: data on the location / index I as compared with other data on the location / index next to I +1, if there is an incompatibility of data, the data in the location I would be exchanged with the data at the location I +1. Then slowly, the data will move toward to the right location. Of this nature, which means that the term bubble bubble is taken. Like the bubbles in soda drinks, which slowly moves up to the top.

      Presented examples of how it works, to 5 pieces of data that is 4, 5, 1, 3, 2. Ordering starts from the first location (I was 1), and compared with the location next to him (I +1 is 2). Because the data 4 and 5 was located at a suitable sequence, was not exchanged. Then check the next location data (I was 2) with the location next to him (I +1 is 3), 5 and 1 data was not suitable, then exchanged. Next location (I was 3) compared with the location next to him (I +1 is 4), data were 5 and 3, does not fit anymore, then exchanged again. So forth, are done to confirmed that all existing data in a suitable location, which is done in a way there is no more exchanges are done. Here is the process of change data for a sample data 4, 5, 1, 3, 2 are:
First iteration (First Pass)

4 5 1 3 2 (suitable)
4 5 1 3 2 (exchange 5 and 1) 4 1 5 3 2
4 1 5 3 2 (exchange 5 and 3) 4 1 3 5 2
4 1 3 5 2 (exchange 5 and 2) 4 1 3 2 5

Second loop (Second Pass)

4 1 3 2 5 (exchange 4 and 1) 1 4 3 2 5
1 4 3 2 5 (exchange 4 and 3) 1 3 4 2 5
1 3 4 2 5 (exchange 4 and 2) 1 3 2 4 5
1 3 2 4 5 (suitable)

Third iteration (Third Pass)

1 3 2 4 5 (suitable)
1 3 2 4 5 (exchange 3 ​​and 2) 1 2 3 4 5
1 2 3 4 5 (suitable)
1 2 3 4 5 (suitable)

Fourth iteration (Fourth Pass)

1 2 3 4 5 (suitable)
1 2 3 4 5 (suitable)
1 2 3 4 5 (suitable)
1 2 3 4 5 (suitable)
See The Code : 
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 30
#define DELAY 10000000
#define TRUE 1
#define FALSE 0
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif
void BubbleSort(int*, int),
    Swap(int*, int*),
        InputOutput(int*, const int, const char),
    Delay(void),
FreeBuffer(int*);
int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Bubble Sort\nMasukkan banyak data [max:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\nData yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        BubbleSort(buffer,max);
        printf("\nData setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    } TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}
void BubbleSort(int* buffer, int max) {
    int i, j;
    for(i = 0; i < max; ++i) {
        for(j = 0; j < max-i; ++j) {
            if(*(buffer+j) > *(buffer+(j+TRUE))) { // if(buffer[j] > buffer[j+TRUE]) {
                Swap((buffer+j),(buffer+(j+TRUE))); // Swap(&(*(buffer+j)),&(*(buffer+(j+TRUE))));
            }
        }
    }
}
void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}
void InputOutput(int* buffer, const int max, const char STAT) {
    int i;
    /* switch(STAT) {
        case(INPUT) : {
            for(i = 0; i < max; ++i) {
                printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
                scanf("%d",&buffer[i]);
                fflush(stdin);
            }
        } break;
        case(OUTPUT) : {
            for(i = 0; i < max; ++i) {
                printf("%d ",buffer[i]);
                Delay();
            }
        } break;
        default :
            break;
    } */
    if('i' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}
void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}
void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}
See The Out put :

 

Quick Sort In C language

One of the sorting algorithm that is not less important to learn the Quick Sort algorithm. This algorithm is popularly used in the programming world. Therefore, I will explain a little about the Quick Sort algorithm in C + + programming language.

Quick Sort algorithm discovered by C.A.R Hoare. Quick sort, as the name suggests, is claimed as a sorting algorithm that is faster than other sorting algorithms.

However, this algorithm, according to my own too, is considered quite difficult to be understood than others, due to master these algorithms, necessary knowledge about algorithms and patterns of recursive divide-and-concuer. What the hell is that? Below I ulaskan little (very little:)) on both schemes.
Divide
Sorting data group into two sub-groups of data.
Conquer
Sort the elements in the sub-series recursively.
Recursive
The method in which the contents of a function contains function calls itself.
For more details can be seen in the syntax below:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define INPUT 'i'
#define OUTPUT 'o'
#define TRUE 1
#define FALSE 0
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

int ChoosePivot(int, int);
    void InputOutput(int*, const int, const char),
        QuickSort(int*, int, int),
    Swap(int*, int*),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Quick Sort [Ascending]\nJumlah data [MAX:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\n1. Data yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        QuickSort(buffer,FALSE,(max-TRUE));
        printf("\n2. Data setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void InputOutput(int* buffer, int max, const char STAT) {
    int i;
    if(INPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if(OUTPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
        }
    }
}

int ChoosePivot(int top, int bottom) {
    return((top+bottom)/2);
}

void QuickSort(int* buffer, int bottom, int top) {
    int i, j, k, pivot; // m = bottom, n = top;
    if(bottom < top) {
        pivot = ChoosePivot(bottom,top);
        Swap(&buffer[bottom],&buffer[pivot]);
        i = bottom+TRUE; j = top; k = buffer[bottom];
        while(i <= j) {
            while((i <= top) && (buffer[i] <= k)) {
                ++i;
            }
            while((j >= bottom) && (buffer[j] > k)) {
                --j;
            }
            if(i < j) {
                Swap(&buffer[i],&buffer[j]);
            }
        }
        Swap(&buffer[bottom],&buffer[j]);
        QuickSort(buffer,bottom,(j-TRUE));
        QuickSort(buffer,(j+TRUE),top);
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}
See The Out Put :

Selection Sort With C language

Selection sort is a combination of searching and sorting. A sorting algorithm that repeatedly search for items that have not been sorted and find at least one to be included in the final location. This method has the concept of selecting data for maximum / minimum of an array data set L, then put the data to the last element of the earliest appropriate or desirable sequencing. Data maximum / minimum is obtained, exiled to another place, and not included in the process of finding the maximum data / next minimum.

This sorting algorithm selection sort algorithms including difficult divided / combined easily (hard split / easy join). From pengurutannya process, Selection of this sort has two variants, namely:

Sort Maximum
choose the maximum data from an array of data collection, and then put the data to the final element of the earliest appropriate or desirable sequencing. Data maximum / minimum is obtained, "isolated" and not included in the process of finding the next maximum data.

Minimum Sort
select a minimum of data from an array of data collection, and then put the data to the last element of the earliest appropriate or desirable sequencing. Minimum data obtained, the "isolated" and not included in the process of finding the next minimum data.

See The Code :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define DELAY 10000000
#define TRUE 1
#define FALSE 0
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

void SelectionSort(int*, int),
    Swap(int*, int*),
        InputOutput(int*, const int, const char),
    Delay(void),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 3");
    int *buffer = NULL, max;
    printf("Implementation Selection Sort\nEnter Many Data [max:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\nData yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        SelectionSort(buffer,max);
        printf("\nData setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void SelectionSort(int* buffer, int max) {
    int i, j, min;
    for(i = 0; i < max-TRUE; ++i) {
        min = i;
        for(j = i+TRUE; j < max; ++j) {
            if(buffer[min] > buffer[j]) {
                min = j;
            }
        }
        Swap(&buffer[i],&buffer[min]);
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void InputOutput(int* buffer, const int max, const char STAT) {
    int i;
    if('i' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}

void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}
See The Out Put :

See The Process
 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank