<input>
n
nxn matrix
16
387 120 190 124 376 339 452 425 126 11 289 327 353 451 295 119
204 487 377 332 45 432 225 153 420 85 318 276 143 315 323 327
394 113 373 63 221 425 93 425 329 389 457 141 204 359 7 459
90 461 170 66 388 154 381 445 134 134 333 156 174 63 198 239
288 473 284 22 455 295 156 375 430 192 197 0 146 42 478 382
48 130 43 467 29 316 421 185 405 130 98 435 467 56 365 313
268 175 271 449 413 215 1 371 143 252 185 448 412 465 88 315
481 258 282 49 184 449 225 109 298 237 80 283 187 361 378 168
240 371 59 135 389 381 124 190 91 175 381 481 399 109 456 278
104 380 52 385 324 207 407 149 491 100 434 215 261 79 21 424
327 178 162 425 32 305 458 13 4 336 233 134 467 387 35 390
340 303 416 183 84 263 216 436 471 228 197 393 276 230 15 500
450 291 173 5 264 321 69 124 131 385 367 97 324 326 216 363
17 405 0 407 359 14 336 91 136 9 288 131 379 352 217 4
250 478 473 7 249 394 258 190 218 443 99 2 349 477 213 307
181 239 438 164 193 383 181 143 492 498 217 80 268 207 308 350
<output>
sorting된 순서
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int arr[100][100];
int n;
void sort(int row,int type)
{
int i,j,temp;
if (type == 0)
{
for (i=0; i<n;i++)
{
for (j = i%2; j<n; j+=2)
{
if (j+1 < n && arr[row][j] > arr[row][j+1])
{
temp = arr[row][j];
arr[row][j] = arr[row][j+1];
arr[row][j+1] = temp;
}
}
}
}
else
{
for (i=0; i<n;i++)
{
for (j = i%2; j<n; j+=2)
{
if (j+1 < n && arr[row][j] < arr[row][j+1])
{
temp = arr[row][j];
arr[row][j] = arr[row][j+1];
arr[row][j+1] = temp;
}
}
}
}
}
void sortcol(int col)
{
int i,j,temp;
for (i=0; i<n;i++)
{
for (j = i%2; j<n; j+=2)
{
if (j+1 < n && arr[j][col] > arr[j+1][col])
{
temp = arr[j][col];
arr[j][col] = arr[j+1][col];
arr[j+1][col] = temp;
}
}
}
}
int main(void)
{
int i,j;
int logn;
// n
// nxn matrix
FILE* f = fopen ("input3.txt","r");
fscanf (f,"%d",&n);
for (i=0;i<n;i++){
for (j=0;j<n;j++)
fscanf (f,"%d",&arr[i][j]);}
logn = log( (double)n ) / log( (double)2 );
for (i=0;i<logn;i++)
{
for (j=0;j<n;j++)
{
sort(j,j%2);
}
for (j=0;j<n;j++)
{
sortcol(j);
}
}
for (j=0;j<n;j++)
{
sort(j,0);
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf ("%3d ",arr[i][j]);
}
printf ("\n");
}
}