Student Of Fortune

Bubble Sort In C Language

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

 

2 comments on Bubble Sort In C Language :

Eran Smith said... September 12, 2011 at 6:11 AM

Nice work the article. thanks for sharing it.

.net Obfuscator

Adobe Courses said... June 22, 2012 at 12:14 AM

Bubble sorting is a easy sorting method in which we sort the component of the list by forming pairs of adjacent component.The smaller element bubble to the top is another way to see Bubble algorithm.

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank