Skip to content

Instantly share code, notes, and snippets.

@devetude
Created June 12, 2017 03:13
Show Gist options
  • Save devetude/74b5b33833fe326a7a82cccf3cdd512c to your computer and use it in GitHub Desktop.
Save devetude/74b5b33833fe326a7a82cccf3cdd512c to your computer and use it in GitHub Desktop.
버블 정렬 (Bubble Sort)
/**
* 버블 정렬 (Bubble Sort)
*
* @author devetude
*/
public class Main {
/**
* 메인 메소드
*
* @param args
*/
public static void main(String args[]) {
int[] arr = { 40, 50, 10, 90, 20 };
int last = arr.length - 1;
for (int i = 0; i < last; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
int tmp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = tmp;
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("[ ");
for (int i = 0; i < last; i++) {
sb.append(arr[i]).append(", ");
}
sb.append(arr[last]).append(" ]");
System.out.println(sb.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment