정렬 알고리즘
Bubble Sort 동작 방식
package org.agorithm.sort;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class BubbleSort {
@Test
public void executeCase1() {
System.out.println("bubble sort");
int[] array = {0, 3, 7, 4, 5, 1, -1};
this.bubbleSort(array);
System.out.println(Arrays.toString(array));
}
public void bubbleSort(int[] array) {
for (int i = 0; i < array.length-1; i++) {
boolean swap = false;
System.out.println("==i : " + i);
for (int j = 0; j < array.length-1-i; j++) {
System.out.println("j :" + j);
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swap = true;
}
}
if(!swap) {
System.out.println("break");
break;
}
}
}
}
- 결과
[-1, 0, 1, 3, 4, 5, 7] / 빅오는 O(N^2)
Insert Sort 동작 방식