Skip to content

Instantly share code, notes, and snippets.

@wicksome
Created October 1, 2015 15:28
Show Gist options
  • Save wicksome/9ce7db91c3cb24cc0591 to your computer and use it in GitHub Desktop.
Save wicksome/9ce7db91c3cb24cc0591 to your computer and use it in GitHub Desktop.
버블정렬
package kr.opid.sorting;
/**
* @author Yeong-jun
*/
public class BubbleSort {
public static void main(String[] args) {
int[] array = new int[10];
array[0] = 33;
array[1] = 11;
array[2] = 99;
array[3] = 1;
array[4] = 22;
array[5] = 88;
array[6] = 55;
array[7] = 44;
array[8] = 66;
array[9] = 77;
System.out.println("before)");
displayArray(array);
System.out.println();
System.out.println("after)");
bubbleSort(array);
}
static int[] bubbleSort(int[] arr) {
for (int i = arr.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
return arr;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* 배열 출럭
*
* @param arr
*/
static void displayArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment