Bubble Sort

: compares all the element one by one and sort them based on their values.

 

 

이미지참조

https://user-images.githubusercontent.com/1997137/63668342-6c5ff680-c809-11e9-80f6-fa7e80959dec.png

 

public class _3BubbleSort {
	public static int[] bubbleSort(int[] list) {
		for (int i = 0; i < list.length; i++) {//처음 고정의 역할
			for (int j = 0; j < list.length - 1 - i; j++) {
				if (list[j] > list[j + 1]) {//j를 기준점으로 j+1의 자리와 비교
					int temp = list[j];
					list[j] = list[j + 1];
					list[j + 1] = temp;
				}
			}
		}
		return list;
	}

	public static void main(String[] args) {
		int [] list= {10,44,3,64,6,21,68,365,99};
		System.out.println(Arrays.toString(list));
		bubbleSort(list);
		System.out.println(Arrays.toString(list));
	}
}