20220209 º¹½À
°ª À§Ä¡ ¹Ù²Ù´Â ¹ý
¿À¸§Â÷¼ø if ( a>b ) ³»¸²Â÷¼ø if ( a<b )
temp=a;
a=b;
b=temp;
À̺а˻öÀÇ ÀüÁ¦ Á¶°Ç => ¹è¿ÀÌ Á¤·ÄµÇ¾îÀÖ¾î¾ß ÇÔ.
¹è¿ÀÇ Áß°£°ªÀ» ãÀ»°ª°ú ºñ±³ÇÏ¿© Á¶°Ç¿¡ ¸ÂÁö ¾Ê´Â ¹ÝÀýÀ» ¹ö¸².
°è¼Ó ¹Ýº¹ÇÏ¿© °ªÀ» ãÀ½. (½Ã°£´ÜÃà °¡´É) (½ÇÇà Àü Á¤·Ä Çʼö)
{2,1,4,5,3}¹è¿ÀÇ Á¤·ÄÀ» À§ÇØ °ªÀ» ±³È¯ÇÏ´Â ÁøÇà ¼ø¼Ç¥
Ƚ¼ö |
2 |
1 |
4 |
5 |
3 |
1 |
1 |
2 |
4 |
5 |
3 |
2 |
1 |
2 |
4 |
5 |
3 |
3 |
1 |
2 |
4 |
5 |
3 |
4 |
1 |
2 |
4 |
5 |
3 |
5 |
1 |
2 |
4 |
5 |
3 |
6 |
1 |
2 |
4 |
5 |
3 |
7 |
1 |
2 |
4 |
5 |
3 |
8 |
1 |
2 |
4 |
5 |
3 |
9 |
1 |
2 |
3 |
5 |
4 |
10 |
1 |
2 |
3 |
4 |
5 |
tempº¯¼ö¸¦ »ç¿ëÇÏ¿© a,bÀÇ °ªÀ» ±³È¯½ÃÅ°±â
class T0209_31 //temp¸¦ ÀÌ¿ëÇÏ¿© a¿Íb¸¦ ±³È¯½ÃÅ°±â
{
public static void main(String[] args)
{
int temp, a, b;
a=10;
b=20;
System.out.println("a: "+a+" b: "+b);
temp=a;
a=b;
b=temp;
System.out.println("a: "+a+" b: "+b);
}
}
¼±ÅÃÁ¤·Ä ¾Ë°í¸®ÁòÀ» do ~ while¹®À¸·Î Ç®¾îº¸±â
import java.util.*;
class T0209_32 //¼±ÅÃÁ¤·Ä ¾Ë°í¸®Áò(do while ¹®)
{
static void Print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.printf("[%2d]",arr[i]);
}
System.out.println();
}
static void Sort(int[] DATA){
int i,J,K;
int cnt=0;
int tnt=0;
i=-1;
do{
cnt++;
i=i+1;
J=i;
do{
tnt++;
J++;
if(DATA[i]>DATA[J]){
K=DATA[i];
DATA[i]=DATA[J];
DATA[J]=K;
}
System.out.println("Ä«¿îÆ®: "+cnt+" = "+DATA[0]+"-"+DATA[1]+"-"+DATA[2]+"-"+DATA[3]+"-"+DATA[4]);
}
while (J<4);
}
while (i<3);
System.out.println("ÃÑ Ä«¿îÆ® Ƚ¼ö: "+tnt);
}
public static void main(String[] args)
{
int[] DATA=new int[]{2,1,4,5,3};
System.out.println("Á¤·Ä Àü");
Print(DATA);//Á¤·Ä Àü ¹è¿ Ãâ·ÂÇϱâ
System.out.println("Á¤·Ä ÈÄ");
Sort(DATA);//¹è¿À» Á¤·Ä½ÃÅ°±â
}
}
¼±ÅÃÁ¤·Ä ¾Ë°í¸®ÁòÀ» for¹®À¸·Î Ç®¾îº¸±â
class T0209_33 //¼±Åà Á¤·Ä ¾Ë°í¸®Áò(for ¹®)
{
static void Print(int[] DATA){
for(int i=0;i<DATA.length;i++){
System.out.printf("[%2d]",DATA[i]);
}System.out.println();
}
static void Sort(int[] DATA){
int cnt=0;
int tnt=0;
for(int i=0;i<4;i++){
cnt++;
for(int j=i+1;j<5;j++){
tnt++;
if(DATA[i]>DATA[j]){
int k=DATA[i];
DATA[i]=DATA[j];
DATA[j]=k;
}System.out.println("Ä«¿îÆ®: "+cnt+", "+DATA[0]+"-"+DATA[1]+"-"+DATA[2]+"-"+DATA[3]+"-"+DATA[4]);
}
}System.out.println("ÃÑ È¸¼ö: "+tnt);
}
public static void main(String[] args)
{
int[] DATA=new int[]{2,1,4,5,3};
System.out.println("Á¤·Ä Àü");
Print(DATA);
System.out.println("Á¤·Ä ÈÄ");
Sort(DATA);
}
}