Created
September 17, 2018 13:17
-
-
Save developer-sdk/8cc7b8b32b514ec3d2c0f64c644f9dad to your computer and use it in GitHub Desktop.
buble sort
This file contains hidden or 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
| import java.util.Arrays; | |
| /** | |
| * 거품정렬 | |
| * 두 인접한 원소를 비교하여 확인하는 처리하는 정렬 | |
| * | |
| * @author User | |
| * | |
| */ | |
| public class BubbleSort { | |
| static int[] array = { 9, 3, 5, 4, 6, 1, 8, 2 }; | |
| public static void sort(int[] array) { | |
| for (int i = 0; i < array.length; i++) { | |
| for (int j = 1; j < array.length - i; j++) { | |
| // 현재원소와 바로 앞 원소를 비교하여 큰쪽을 뒤로 넘긴다. | |
| if (array[j - 1] > array[j]) | |
| swap(j - 1, j); | |
| } | |
| } | |
| System.out.println(Arrays.toString(array)); | |
| } | |
| private static void swap(int i, int j) { | |
| int temp = array[i]; | |
| array[i] = array[j]; | |
| array[j] = temp; | |
| } | |
| public static void main(String[] args) { | |
| BubbleSort.sort(array); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment