Sorting Array 2 Dimensi

Algoritma :

Deklarasai
a[10][10] : int

cur,cur2,z,temp : int

Deskripsi
read (z)

for i <– 0 to z do

for j <– 0 to z do

write( [i][j] )

end for

for i <– 0 to z do

for j<– 0 to z do

cur=i

cur2=j

for k <– 0 to z do

for l <– 0 to z do

if (a [cur][cur2] < a[k][l]){

cur=k

cur2=l

end if

temp=a[i][j]

a[i][j]=a[cur][cur2]

a[cur][cur2]=temp

end for

end for

for i <– 0 to z do

for j <– 0 to z do

write ( a[i][j] )

Implementasi program dalam bahasa C++ :


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

        int a[10][10];
          int cur,cur2,z,temp;

    cout<<"Masukan data: ";
    cin>>z;
    
    for(int i=0;i<z;i++)
    for(int j=0;j<z;j++)
    {
        cout<<"Data["<<i<<"]["<<j<<"]:";
        cin>>a[i][j];
    }
    for(int i=0;i<z;i++)
    for(int j=0;j<z;j++)
    {
        cur=i;
        cur2=j;
        for(int k=0;k<z;k++)
        for(int l=0;l<z;l++)
              {
                   if(a[cur][cur2]<a[k][l])
                   { 
                        cur=k;
                        cur2=l;
                   }
                   temp=a[i][j];     
        a[i][j]=a[cur][cur2];
        a[cur][cur2]=temp;
              }   
    }   
    for(int i=0;i<z;i++)
    {
    for(int j=0;j<z;j++)
    {
       cout<<" "<<a[i][j];
    }
   cout<<endl;
    }
         

    system("PAUSE");
    return EXIT_SUCCESS;
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s