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));
}
}
'java' 카테고리의 다른 글
Java/Recursion/재귀호출 (0) | 2020.01.28 |
---|---|
Java/Linear Search/선형검색 (0) | 2020.01.28 |
Java/Binary Search/이진탐색 (0) | 2020.01.28 |
Java/Selection Sort/선택정렬 (0) | 2020.01.28 |
Java/Local Variable and Static Variable/ 지역변수와 스태틱변수의 비교 (0) | 2020.01.28 |