Student Of Fortune

Selection Sort With C language

Share on :
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

0 comments on Selection Sort With C language :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank