#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 InsertionSort(int*, const int); void InputOutput(int*, const int, const char); void Delay(void); void FreeBuffer(int*); int main(int argc, char *argv[]) { system("COLOR 5"); int *buffer = NULL, max; printf("Implementasi Insertion 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); InsertionSort(buffer,max); printf("\nData setelah disorting : "); InputOutput(buffer,max,OUTPUT); FreeBuffer(buffer); } TRACE_LINE; getch(); fflush(stdin); return(EXIT_SUCCESS); } void InsertionSort(int* buffer, const int max) { int i, j, tmp; for(i = 1; i < max; ++i) { tmp = buffer[i]; j = i-TRUE; while((j >= FALSE) && (buffer[j] > tmp)) { buffer[j+TRUE] = buffer[j]; --j; } buffer[j+TRUE] = 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; }
Tuesday, November 29, 2011
Insertion Sort dalam bahasa C
Subscribe to:
Post Comments (Atom)
Artikelnyya bermanfaat kak, ini saya juga punya artikel tentang Insertion Sort, semoga dapat saling melengkapi
ReplyDeleteInsertion Sort dalam Bahasa C (Materi + Koding)