This time I will write the code about shellsort.
ShellSort.java
public class ShellSort {
	public static void sort(int[] array) {
		int h;
		for(h=1;h<array.length/9;h=h*3+1) {
		}
		for(;h>0;h/=3) {
			for(int i=h;i<array.length;i++) {
				int j=i;
				while(j>=h && array[j-h]>array[j]) {
					int temp = array[j];
					array[j] = array[j-h];
					array[j-h] = temp;
					j -= h;
				}
			}
		}
	}
	public static void main(String args[]) {
		int[] array = {3,2,4,5,1};
		sort(array);
		for(int i=0;i<array.length;i++) {
			System.out.print(array[i]);
		}
	}
}
Next time I'll try quicksort.
Recommended Posts