|
1 | 1 | /*
|
2 |
| -Shellsort is based on the insertion sort. |
3 |
| -
|
4 |
| -Here’s the problem with the insertion sort. Suppose a small item is on the far right, where the large items should be. |
5 |
| -To move this small item to its proper place on the left, all the intervening items (between the place where it |
6 |
| -is and where it should be) must be shifted one space right. This step takes close to N copies, just for one item. |
7 |
| -Not all the items must be moved a full N spaces, but the average item must be moved N/2 spaces, which takes N times |
8 |
| -N/2 shifts for a total of (N^2)/2 copies. Thus, the performance of insertion sort is O(N^2) |
| 2 | + Solved By: Sandeep Ranjan (1641012352) |
9 | 3 | */
|
10 | 4 |
|
11 |
| -class Shell |
12 |
| -{ |
13 |
| - static void printArray(int arr[]) |
14 |
| - { |
15 |
| - int n = arr.length; |
16 |
| - for (int i=0; i<n; ++i) |
17 |
| - System.out.print(arr[i] + " "); |
18 |
| - System.out.println(); |
19 |
| - } |
20 |
| - |
21 |
| - int sort(int arr[]) |
22 |
| - { |
23 |
| - int n = arr.length; |
24 |
| - |
25 |
| - for (int gap = n/2; gap > 0; gap /= 2) |
26 |
| - { |
27 |
| - for (int i = gap; i < n; i += 1) |
28 |
| - { |
29 |
| - int temp = arr[i]; |
30 |
| - int j; |
31 |
| - for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) |
32 |
| - arr[j] = arr[j - gap]; |
33 |
| - |
34 |
| - arr[j] = temp; |
35 |
| - } |
36 |
| - } |
37 |
| - return 0; |
38 |
| - } |
39 |
| - |
40 |
| - public static void main(String args[]) |
41 |
| - { |
42 |
| - int arr[] = {12, 34, 54, 2, 3}; |
43 |
| - System.out.println("Array before sorting"); |
44 |
| - printArray(arr); |
45 |
| - |
46 |
| - ShellSort ob = new ShellSort(); |
47 |
| - ob.sort(arr); |
48 |
| - |
49 |
| - System.out.println("Array after sorting"); |
50 |
| - printArray(arr); |
51 |
| - } |
| 5 | +class ArrayShell { |
| 6 | + private int[] a; |
| 7 | + private int nElems; |
| 8 | + |
| 9 | + public ArrayShell(int max) { |
| 10 | + a = new int[max]; |
| 11 | + nElems = 0; |
| 12 | + } |
| 13 | + |
| 14 | + public void insert(int value) { |
| 15 | + a[nElems] = value; |
| 16 | + nElems++; |
| 17 | + } |
| 18 | + |
| 19 | + public void display() { |
| 20 | + for(int j=0; j<nElems; j++) |
| 21 | + System.out.print(a[j] + " "); |
| 22 | + System.out.println(""); |
| 23 | + } |
| 24 | + |
| 25 | + public void shellSort() { |
| 26 | + // Knuth’s Interval Sequence |
| 27 | + int h = 1; |
| 28 | + while(h <= nElems/3) |
| 29 | + h = h*3 + 1; |
| 30 | + |
| 31 | + while(h > 0) { |
| 32 | + for(int out=h; out<nElems; out++) { |
| 33 | + int temp = a[out]; |
| 34 | + int in = out; |
| 35 | + |
| 36 | + while(in > h-1 && a[in - h] >= temp) { |
| 37 | + a[in] = a[in-h]; |
| 38 | + in -= h; |
| 39 | + } |
| 40 | + |
| 41 | + a[in] = temp; |
| 42 | + } |
| 43 | + |
| 44 | + // Reducing Knuth’s |
| 45 | + h = (h-1) / 3; |
| 46 | + } |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class ShellSort { |
| 53 | + public static void main(String[] args) { |
| 54 | + int maxSize = 11; |
| 55 | + ArrayShell arr = new ArrayShell(maxSize); |
| 56 | + |
| 57 | + arr.insert(77); |
| 58 | + arr.insert(99); |
| 59 | + arr.insert(44); |
| 60 | + arr.insert(55); |
| 61 | + arr.insert(22); |
| 62 | + arr.insert(88); |
| 63 | + arr.insert(11); |
| 64 | + arr.insert(00); |
| 65 | + arr.insert(66); |
| 66 | + arr.insert(33); |
| 67 | + |
| 68 | + arr.shellSort(); |
| 69 | + arr.display(); |
| 70 | + |
| 71 | + } |
52 | 72 | }
|
0 commit comments