forked from Pradeepsingh61/DSA_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
173 lines (142 loc) · 5.92 KB
/
ShellSort.java
File metadata and controls
173 lines (142 loc) · 5.92 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Algorithm: Shell Sort
* Description: Advanced insertion sort that uses gap sequences for improved performance
* Time Complexity: O(n^3/2) worst case, O(n log n) average case (depends on gap sequence)
* Space Complexity: O(1) - in-place sorting algorithm
*
* @author Pasan Sulakshana Rathnayake
* @version 1.0
* @since 2025-10-04
*/
import java.util.*;
public class ShellSort {
/**
* Sorts an array using shell sort algorithm with Knuth's gap sequence.
*
* @param arr the array to be sorted
* @throws IllegalArgumentException if array is null
*/
public static void shellSort(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
if (arr.length <= 1) {
return; // Already sorted
}
int n = arr.length;
// Start with Knuth's gap sequence: h = 3h + 1
// This gives us gaps like: 1, 4, 13, 40, 121, 364, ...
int gap = 1;
while (gap < n / 3) {
gap = 3 * gap + 1;
}
// Perform shell sort with decreasing gaps
while (gap >= 1) {
// Perform insertion sort for elements at gap distance
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
// Shift elements that are greater than temp
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
// Place temp in its correct position
arr[j] = temp;
}
// Reduce gap for next iteration
gap = gap / 3;
}
}
/**
* Alternative shell sort implementation using Shell's original gap sequence.
* This version uses gaps: n/2, n/4, n/8, ..., 1
*
* @param arr the array to be sorted
* @throws IllegalArgumentException if array is null
*/
public static void shellSortOriginal(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
if (arr.length <= 1) {
return;
}
int n = arr.length;
// Start with gap = n/2 and reduce by half each time
for (int gap = n / 2; gap > 0; gap /= 2) {
// Perform insertion sort for elements at gap distance
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
// Shift elements that are greater than temp
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
// Place temp in its correct position
arr[j] = temp;
}
}
}
/**
* Test the shell sort implementation.
*/
public static void main(String[] args) {
System.out.println("SHELL SORT ALGORITHM TESTING");
System.out.println("=============================");
// Test case 1: Regular array
int[] test1 = {64, 34, 25, 12, 22, 11, 90, 88, 76, 50, 42};
System.out.println("\n1. Regular array test:");
System.out.println("Original: " + Arrays.toString(test1));
shellSort(test1.clone());
System.out.println("Sorted: " + Arrays.toString(test1));
shellSort(test1);
System.out.println("Verified: " + Arrays.toString(test1));
// Test case 2: Already sorted
int[] test2 = {1, 2, 3, 4, 5, 6, 7, 8};
System.out.println("\n2. Already sorted array:");
System.out.println("Original: " + Arrays.toString(test2));
shellSort(test2);
System.out.println("Sorted: " + Arrays.toString(test2));
// Test case 3: Reverse sorted
int[] test3 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
System.out.println("\n3. Reverse sorted array:");
System.out.println("Original: " + Arrays.toString(test3));
shellSort(test3);
System.out.println("Sorted: " + Arrays.toString(test3));
// Test case 4: Array with duplicates
int[] test4 = {5, 2, 8, 2, 9, 1, 5, 5, 2};
System.out.println("\n4. Array with duplicates:");
System.out.println("Original: " + Arrays.toString(test4));
shellSort(test4);
System.out.println("Sorted: " + Arrays.toString(test4));
// Test case 5: Single element
int[] test5 = {42};
shellSort(test5);
assert test5[0] == 42;
// Test case 6: Empty array
int[] test6 = {};
shellSort(test6);
assert test6.length == 0;
// Test case 7: Two elements
int[] test7 = {9, 3};
System.out.println("\n5. Two elements:");
System.out.println("Original: " + Arrays.toString(test7));
shellSort(test7);
System.out.println("Sorted: " + Arrays.toString(test7));
// Test case 8: Compare gap sequences
int[] testOriginal = {64, 34, 25, 12, 22, 11, 90, 88, 76, 50, 42};
int[] testKnuth = testOriginal.clone();
System.out.println("\n6. Comparing gap sequences:");
System.out.println("Original array: " + Arrays.toString(testOriginal));
shellSortOriginal(testOriginal);
System.out.println("Shell's gaps: " + Arrays.toString(testOriginal));
shellSort(testKnuth);
System.out.println("Knuth's gaps: " + Arrays.toString(testKnuth));
System.out.println("\nAll test cases passed!");
System.out.println("Shell Sort advantages:");
System.out.println(" - Better than insertion sort for larger arrays");
System.out.println(" - In-place sorting (O(1) space complexity)");
System.out.println(" - Adaptive - performs well on partially sorted data");
System.out.println(" - Gap sequence affects performance significantly");
}
}