1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.gaurangjadia.code.sorting; import java.util.Random; public class ShellSort { public Random rdmNumber = new Random(); public int[] intArray = new int[10]; public static void main(String[] args) { ShellSort object = new ShellSort(); object.setRandomArray(object.intArray); System.out.println("Randomized Array:"); object.printArray(object.intArray); object.intArray = object.doShellSort(object.intArray); System.out.println("Sorted Array:"); object.printArray(object.intArray); } public int getRandomNumber(){ int intRandomNumber; intRandomNumber = rdmNumber.nextInt(100); return intRandomNumber; } public void setRandomArray(int[] array){ for(int i=0; i < array.length; i++){ array[i] = getRandomNumber(); } } public int[] doShellSort(int[] array){ int n = array.length; int ino = (int) n / 2; while(ino > 0){ for(int i = ino; i < (n - 1); i++){ int temp = array[i]; int j = i; while(j >= ino && array[j - ino] > temp){ array[j] = array[j - ino]; j = j - ino; array[j] = temp; } } ino = (int) (ino / 2.2); } return array; } public void printArray(int[] array){ for(int i=0; i < array.length; i++){ System.out.print(array[i] + ", "); } System.out.println("\n"); } } |