Last active
April 21, 2020 17:53
-
-
Save wicksome/fa1d90d008ddf427479a to your computer and use it in GitHub Desktop.
Implementation of ShellSort in Java.
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
| package kr.opid.sorting; | |
| public class ShellSort { | |
| 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)"); | |
| shellSort(array); | |
| } | |
| static int[] shellSort(int[] arr) { | |
| int gap = 1; | |
| while (gap <= arr.length / 3) { | |
| gap = gap * 3 + 1; // 크누스 간격순서 | |
| } | |
| while (gap > 0) { | |
| for (int i = gap; i < arr.length; i++) { | |
| int tmp = arr[i]; | |
| int idx = i; | |
| while (idx > gap - 1 && arr[idx - gap] >= tmp) { | |
| arr[idx] = arr[idx - gap]; | |
| idx -= gap; | |
| } | |
| arr[idx] = tmp; | |
| } | |
| gap = (gap - 1) / 3; | |
| } | |
| 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
Why does it not show after??