Skip to content

Instantly share code, notes, and snippets.

@wicksome
Created October 1, 2015 15:29
Show Gist options
  • Save wicksome/abd695456666a3a0ac68 to your computer and use it in GitHub Desktop.
Save wicksome/abd695456666a3a0ac68 to your computer and use it in GitHub Desktop.
삽입정렬
package kr.opid.sorting;
public class InsertionSort {
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)");
insertionSort(array);
}
static int[] insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i];
int j = i;
while (j > 0 && arr[j - 1] >= tmp) {
arr[j] = arr[j - 1];
--j;
}
arr[j] = tmp;
}
return arr;
}
/**
* 배열 출럭
*
* @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