Program Kumpulan 4 macam sorting array (bubble, exchange, selection, insertion)

Sebenarnya ini adalah source kode untuk tugas kuliah saya. Mungkin masih banyak sekali kekurangannya, maka saya tidak akan bosan untuk meminta kritik dan saran teman-teman reader semua.

#include <stdio.h>
#include <stdlib.h>
void addArray();
void cekArray();
void tampil();
void bubbleSort();
void exchangeSort();
void selectionSort();
void insertionSort();
void resetArray();
int A[100]={'nol'},jml,i,j,k,no,tampung,pos;
int sekunder[100];
void main()
{
    system("cls");
    resetArray();
    char key;
    printf("\n _______PROGRAM PENGURUTAN KODE POS_______\n"
           "\n 1. Masukan data"
           "\n 2. Bubble Sort"
           "\n 3. Exchange Sort"
           "\n 4. Selection Sort"
           "\n 5. Insertion Sort"
           "\n 6. Keluar"
           "\n Perintah : ");
    scanf("%c",&key); system("cls");
    if(key=='1'){addArray();}if(key=='2'){bubbleSort();}
    if(key=='3'){exchangeSort();}if(key=='4'){selectionSort();}
    if(key=='5'){insertionSort();}if(key=='6'){exit(1);}
    else {printf("\n Perintah salah.");getchar();getchar();main();}
}
void tampil(){
    for(k=0;k<jml;k++){printf("[%d] ",A[k]);}
}
void cekArray(){
    if(A[0]=='nol'){printf("\n Array kosong!");
    getchar();getchar();main();}
}
void addArray(){
    printf("\n ______MENAMBAHKAN DATA______\n"
           "\n Berapa data yang ingin diinput? ");
    scanf("%d",&jml);
    for(i=0;i<jml;i++){
        printf(" data ke-%d : ",i+1);
        scanf("%d",&A[i]);
        sekunder[i]=A[i];
    }
    printf("\n Data berhasil diinput : ");
    tampil();getchar();getchar();main();
}
void bubbleSort(){
    cekArray();
    printf("\n ______BUBBLE SORT METODE______\n"
           "\n Data sebelum disorting : "); tampil();
    for(i=1;i<jml;i++){
        printf("\n Proses ke-%d",i);
        for(j=1;j<jml;j++){
            if(A[j-1]>A[j]){
                printf("\n tukar %d <-> %d --->",A[j-1],A[j]);
                tampung=A[j-1];
                A[j-1]=A[j];
                A[j]=tampung;
                tampil();
            } } }
    printf("\n\n Data setelah disorting : ");
    tampil();getchar();getchar();main();
}
void exchangeSort(){
    cekArray();
    printf("\n ______EXCHANGE SORT METODE______\n"
           "\n Data sebelum disorting : ");
           tampil();no=0;
    for(i=jml-1;i>0;i--){
        no++;
        printf("\n\n Proses ke-%d",no);
        for(j=i-1;j>-1;j--){
            if(A[i]<A[j]){
                printf("\n tukar %d <-> %d --->",A[i],A[j]);
                tampung=A[i];
                A[i]=A[j];
                A[j]=tampung; tampil();
            } } }
    printf("\n\n Data setelah disorting : ");
    tampil();getchar();getchar();main();
}
void selectionSort(){
    cekArray();
    printf("\n ______SELECTION SORT METODE______\n"
           "\n Data sebelum disorting : ");
    tampil();no=0;
    for(i=0;i<jml-1;i++){
        pos=i;no++;
        printf("\n_______________________________________"
               "\n______________Proses %2d________________\n",no);
        for(j=0;j<jml;j++){printf("%5d ",j);}
        printf("\n");
        for(j=0;j<jml;j++){printf("%5d ",A[j]);}
        printf("\n\n Pembanding        Pos");
        for(j=i+1;j<jml;j++){
            if(A[pos]<A[j])
            printf("\n %d < %d            [%d]",A[pos],A[j],pos);
            if(A[pos]>A[j]){
            printf("\n %d > %d    <->     [%d]",A[pos],A[j],j);
            pos=j;}
        }
        if(pos!=i){
            printf("\n tukar data ke-%d [%d] dengan data ke-%d [%d]",i,A[i],pos,A[pos]);
            tampung=A[pos];
            A[pos]=A[i];
            A[i]=tampung;
        } }
    printf("\n\n Data setelah disorting : ");
    tampil();getchar();getchar();main();
}
void insertionSort(){
    cekArray();
    printf("\n ______INSERTION SORT METODE______\n"
           "\n Data sebelum disorting : ");
    tampil();
    for(i=1;i<jml;i++){
        tampung=A[i];
        printf("\n____________________________________"
               "\n_____________Proses %2d______________\n",i);
        for(j=0;j<jml;j++){printf("%5d ",j);}
        printf("\n");
        for(j=0;j<jml;j++){printf("%5d ",A[j]);}
        printf("\n yang ditampung : %d\n\n",tampung);
        j=i-1;
        for(k=1;k<=i;k++){
            if(A[k-1]>tampung){
            printf(" %d < %d     Geser data[%d]->data[%d]\n",tampung,A[k-1],j,j+1);
            j--;}
        }
        j=i-1;
        while(A[j]>tampung && j>=0){
            A[j+1]=A[j];
            j--;
        }
        A[j+1]=tampung;
        printf("\n Tempatkan %d pada index[%d]",tampung,j+1);
    }
    printf("\n\n Data setelah disorting : ");
    tampil();getchar();getchar();main();
}
void resetArray(){
    for(i=0;i<jml;i++){
        A[i]=sekunder[i];
    }
}

Kalo ada yang salah beritahu saya ya🙂

About Iin aryani

I don't wanna be a "Copycat" but, something they need to copy.

Posted on 25 April 2012, in Open Source ( C Program ) and tagged . Bookmark the permalink. 5 Comments.

  1. maksi kk, scriptnya sukses lancarr🙂

  2. terima kasih, maaf oot

  3. salam
    teh setelah ane copy k c++, failed di bagian “main”
    mohon pencerahan

Komentar untuk penulis

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: