특별한딸기이야기

자바 코드(병합 프로그램_선택 정렬 추가) 본문

딸기 공부방/JAVA

자바 코드(병합 프로그램_선택 정렬 추가)

특별한녀석 2008. 5. 24. 01:00

package com.tistory.special0strawberry;

public class Run
{
 // 메인 함수
 public static void main(String[] args)
 {
  IntArray a, b, c;
  Sort s = new Sort();
  Add add = new Add();
 
  a = new IntArray();
  b = new IntArray();
 
  System.out.print("첫번째 ");
  a.UserInput();
  s.BubbleSort(a);
  System.out.print("\n첫번째 ");
  a.PrintArray();
 
  System.out.print("\n두번째 ");
  b.UserInput();
  s.SelectSort(b);
  System.out.print("\n두번째 ");
  b.PrintArray();
 
  c = add.ArrayAdd(a, b);
  s.SelectSort(c);
  System.out.print("\n세번째 ");
  c.PrintArray();
 }
}

package com.tistory.special0strawberry;

public class Sort
{
 // 입력값 : 정렬할 IntArray 포인터
 // 버블 정렬을 통해 배열을 정렬한다.
 public void BubbleSort(IntArray a)
 {
  int temp, temp_1, temp_2;
 
  for(temp = 0; temp < a.GetArrayLength() - 1; temp++)
  {
   for(temp_1 = 0; temp_1 < a.GetArrayLength() - temp - 1; temp_1++)
   {
    if(a.GetArrayValue(temp_1) > a.GetArrayValue(temp_1 + 1))
    {
     temp_2 = a.GetArrayValue(temp_1 + 1);
     a.SetArrayValue(temp_1 + 1, a.GetArrayValue(temp_1));
     a.SetArrayValue(temp_1, temp_2);
    }
   }
  }
 }
 
 // 입력값 : 정렬할 IntArray 포인터
 // 선택 정렬을 통해 배열을 정렬한다.
 public void SelectSort(IntArray i)
 {
  int temp, choice, temp_1, temp_2;
 
  for(temp = 0; temp < i.GetArrayLength(); temp++)
  {
   choice = temp;
   for(temp_1 = temp; temp_1 < i.GetArrayLength(); temp_1++)
   {
    if(i.GetArrayValue(temp_1) < i.GetArrayValue(choice))
    {
     choice = temp_1;
    }
   }
   temp_2 = i.GetArrayValue(choice);
   i.SetArrayValue(choice, i.GetArrayValue(temp));
   i.SetArrayValue(temp, temp_2);
  }
 }
}

'딸기 공부방 > JAVA' 카테고리의 다른 글

양력을 음력으로 바꾸는 소스  (0) 2009.04.28
scjp 1.4 지료  (0) 2008.07.03
자바 코드(병합 프로그램)  (0) 2008.05.22
자바 코드(숫자 바꾸기)  (0) 2008.05.22