LOOPING (While)

# include <iostream.h>
# include <conio.h>
int main()
{

#pragma argsused
int main(int argc, char* argv[])
{
        int i,j;
        int x;    // variabel bantu agar L[k] tidak ditimpa (replace) selama pergeseran
        bool ketemu;
 
        int L[10] = {20,15,22,14,12,10,24,19,18,16};
 
        for(i=1;i<=9;i++)
        {
                /* sisipkan L[i] ke dalam bagian yang sudah terurut */
                x = L[i];
 
                /*cari posisi yang tepat untuk x di dalam L[0..i-1] sambil menggeser */
                j=i-1;
                ketemu = false;
 
                while ((j>=0)&&(!ketemu))
                {
                        if(x < L[j])
                        {
                                L[j+1] = L[j];
                                j–;
                        }
                        else
                        {
                                ketemu = true;
                        }
                }
                /* j < 1 or ketemu */
                L[j+1] = x;
        }
 
        for(i=0;i<=9;i++)
        {
                printf(“%d “,L[i]);
        }
 
        getch();
        return 0;
}

Leave a Reply

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: