Rabu, 13 April 2016

no 10

Program mengurutkan data dengan quick sort


#include <stdio.h>
#define MAX 10
#define MaxStack 11
int Data[MAX];
// Prosedur menukar data
void Tukar (int *a, int *b)
{
            int temp;
            temp = *a;
            *a = *b;
            *b = temp;
}
// Prosedur pengurutan metode Quick Sort
 void QuickSortNonRekursif()
{
            struct tump {
            int Kiri;
            int Kanan;
            }
            Tumpukan[MaxStack];
            int i, j, L, R, x, ujung = 1; Tumpukan[1].Kiri = 0;
            Tumpukan[1].Kanan = MAX-1;
            while (ujung!=0){
                        L = Tumpukan[ujung].Kiri;
                        R = Tumpukan[ujung].Kanan;
                        ujung--;
                        while(R > L){
                                    i = L;
                                    j = R;
                                    x = Data[(L+R)/2];
                                                while(i <= j){
                                                while(Data[i] < x)
                                                            i++;
                                                            while(x < Data[j])
                                                                        j--;
                                                                        if(i <= j){
                                                                        Tukar(&Data[i], &Data[j]);
                                                                        i++;
                                                                        j--;
                                                }
                        }
                        if(L < i){
                                    ujung++; Tumpukan[ujung].Kiri = i;
                                    Tumpukan[ujung].Kanan = R;
                        }
                        R = j;
            }
}
}
int main()
{
            int i;
            //Memasukkan data yang belum terurut
            printf("DATA SEBELUM TERURUT : \n");
            for(i=1; i<MAX; i++)
            {
                        printf("Data ke %d : ", i);
                        scanf ("%d", &Data[i]);
            }
            QuickSortNonRekursif();
          
            //Data setelah terurut
            printf("\nDATA SETELAH TERURUT");
            for(i=1; i<MAX; i++)
            {
                        printf("\nData ke %d : %d ", i, Data[i]);
            }
            //scanf("%d");
            return(0);
}

Tidak ada komentar:

Posting Komentar

Refleksi minggu ke 2

Refleksi Pertemuan ke 2 Assalamu'alaikum w. wb., Selamat berjumpa lagi teman-teman, baik saya disini akan menuliskan tentang refl...