algorithm 버블 정렬

Algorithm 2015. 7. 9. 01:30

버블 정렬 - 오름차순

public class Main {
	public static void main(String[] args) {
		int[] a = {31, 41, 6, 26, 41, 58, 99, 85, 59, 14};
		int temp = 0;
		int i = 0, j = 0;
		for(i = 0; i < a.length; i++) {
			for(j=a.length-1; j>i; j--) {
				if(a[j] < a[j-1]) {
					temp = a[j];
					a[j] = a[j-1];
					a[j-1] = temp;
				}
			}
		}
	}
}


버블 정렬 - 내림차순

public class Main {
	public static void main(String[] args) {
		int[] a = {31, 41, 6, 26, 41, 58, 99, 85, 59, 14};
		int temp = 0;
		int i = 0, j = 0;
		for(i = a.length-1; i >= 0; i--) {
			for(j = 0; j<i; j++) {
				if(a[j] <= a[j+1]) {
					temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}
			}
		}
	}
}

'Algorithm' 카테고리의 다른 글

algorithm 병합정렬  (0) 2015.07.09
algorithm 삽입정렬  (0) 2015.07.08
algorithm 문자사각형1  (0) 2015.07.01
: