예제)
public class Test5 {
public static void main(String[] args) {
int[] a={56, 2, 34, 13, 17};
System.out.print("source data : ");
for(int n : a)
System.out.print(n+" ");
System.out.println();
//selection sort (정렬)
int temp;
for(int i=0; i<a.length-1; i++) {
for(int j=i+1; j<a.length; j++) {
if(a[i] > a[j]) {
temp=a[i]; a[i]=a[j]; a[j]=temp;
}
}
}
System.out.print("sort data : ");
for(int n : a)
System.out.print(n+" ");
System.out.println();
}
}
결과)
source data : 56 2 34 13 17
sort data : 2 13 17 34 56
'자바 > 문법' 카테고리의 다른 글
난수(무작위 수) 출력 프로그램 (0) | 2013.05.11 |
---|---|
성적표 출력하는 프로그램 (0) | 2013.05.11 |
배열을 이용한 요일 계산기 (0) | 2013.05.11 |
배열을 사용한 달력만들기 (0) | 2013.05.11 |
배열 사용방법과 향상된 for문 (0) | 2013.05.11 |