Created
June 12, 2017 03:13
-
-
Save devetude/74b5b33833fe326a7a82cccf3cdd512c to your computer and use it in GitHub Desktop.
버블 정렬 (Bubble Sort)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 버블 정렬 (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