diff --git a/Java/algorithms/sorting/shellsorting.java b/Java/algorithms/sorting/shellsorting.java new file mode 100644 index 00000000..00e0dbc2 --- /dev/null +++ b/Java/algorithms/sorting/shellsorting.java @@ -0,0 +1,91 @@ +/* +šŸ“˜ Shell Sort Algorithm + +āœļø Description: +Shell Sort is an optimization over Insertion Sort that allows the exchange of far-apart elements. +It works by sorting elements that are a certain 'gap' apart, reducing the gap in each iteration until it becomes 1. +At that point, the algorithm becomes a regular Insertion Sort, but since the array is already mostly sorted, +it performs much faster. + +šŸ’” Use Cases / Applications: +- Useful for medium-sized datasets +- Works well when memory is limited (in-place sorting) +- Suitable when a simple but efficient improvement over insertion sort is needed + +šŸ“Š Complexity Analysis: +Time Complexity: + • Best Case: O(n log n) + • Average Case: O(n^(3/2)) + • Worst Case: O(n²) +Space Complexity: O(1) + • Only a few temporary variables are used — no extra data structures. + +*/ + +#include +#include +using namespace std; + +/* +šŸ”¹ Function: shellSort +šŸ”¹ Parameters: + arr — reference to the vector of integers to be sorted +šŸ”¹ Description: + Performs Shell Sort on the input array. +*/ +void shellSort(vector &arr) { + int n = arr.size(); + + // Start with a large gap and reduce it each iteration + for (int gap = n / 2; gap > 0; gap /= 2) { + + // Perform a gapped insertion sort for this gap size + for (int i = gap; i < n; i++) { + int temp = arr[i]; + int j; + + // Shift earlier gap-sorted elements until correct location for arr[i] is found + for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { + arr[j] = arr[j - gap]; // Move element ahead by gap + } + + arr[j] = temp; // Insert temp at correct position + } + } +} + +/* +āœ… Test Cases / Examples +Demonstrates sorting functionality on multiple inputs +*/ +int main() { + // Example 1: Random elements + vector arr1 = {12, 34, 54, 2, 3}; + shellSort(arr1); + cout << "Sorted Array 1: "; + for (int x : arr1) cout << x << " "; + cout << endl; + + // Example 2: Already sorted array + vector arr2 = {1, 2, 3, 4, 5}; + shellSort(arr2); + cout << "Sorted Array 2: "; + for (int x : arr2) cout << x << " "; + cout << endl; + + // Example 3: Reverse order + vector arr3 = {9, 8, 7, 6, 5}; + shellSort(arr3); + cout << "Sorted Array 3: "; + for (int x : arr3) cout << x << " "; + cout << endl; + + // Example 4: Single element (edge case) + vector arr4 = {42}; + shellSort(arr4); + cout << "Sorted Array 4: "; + for (int x : arr4) cout << x << " "; + cout << endl; + + return 0; +}