Shell Sort is an improved version of Insertion Sort that works faster by comparing elements that are far apart before comparing nearby elements. Instead of sorting the array step by step from the beginning, Shell Sort first sorts elements at a certain gap and then reduces that gap until the array is fully sorted. This simple idea makes a big difference in performance, especially for medium-sized datasets.
with hands-on learning.
get the skills and confidence to land your next move.
This algorithm matters because it is easy to understand, simple to implement, and much faster than basic sorting methods like Bubble Sort or plain Insertion Sort in many cases. Shell Sort is often used in learning environments, embedded systems, and situations where memory usage should stay low. For beginners learning TypeScript, Shell Sort is a great way to understand how small changes in logic can greatly improve efficiency.
Program 1: Basic Shell Sort Using Standard Gap Reduction
This program shows the most common way to implement Shell Sort in TypeScript. It uses a simple gap reduction strategy by dividing the gap by two on each pass.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let data: number[] = [23, 12, 1, 8, 34, 54, 2, 3];
console.log("Sorted array:", shellSort(data));This program works by sorting elements that are far apart first and then slowly reducing the gap. Beginners can think of this as cleaning a messy room by first organizing big sections and then fixing small details. This approach makes the algorithm faster than simple insertion-based sorting.
Program 2: Shell Sort Using While Loops Only
This version uses while loops instead of for loops to control the sorting process. It helps beginners understand that algorithms are flexible and not limited to one loop style.
function shellSortWhile(arr: number[]): number[] {
let n = arr.length;
let gap = Math.floor(n / 2);
while (gap > 0) {
let i = gap;
while (i < n) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
i++;
}
gap = Math.floor(gap / 2);
}
return arr;
}
let values: number[] = [45, 23, 11, 89, 77, 98, 4, 28];
console.log("Sorted array:", shellSortWhile(values));This approach produces the same result as the basic version but uses a different control structure. It is useful for beginners who want to practice logic building and understand how loop conditions affect execution.
Program 3: Shell Sort with Custom Gap Sequence
This program uses a predefined gap sequence instead of dividing by two each time. This can sometimes improve performance.
function shellSortCustomGap(arr: number[]): number[] {
let gaps = [5, 3, 1];
let n = arr.length;
for (let gap of gaps) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let nums: number[] = [64, 34, 25, 12, 22, 11, 90];
console.log("Sorted array:", shellSortCustomGap(nums));This program shows beginners that gap selection matters in Shell Sort. It encourages experimentation and helps learners see how different gap values affect the sorting process.
Program 4: Shell Sort Using Functional Style
This version keeps the logic clear while using modern TypeScript features for better readability.
function shellSortFunctional(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap >>= 1) {
arr.forEach((_, i) => {
if (i >= gap) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
});
}
return arr;
}
let list: number[] = [19, 2, 31, 45, 6, 11, 121, 27];
console.log("Sorted array:", shellSortFunctional(list));This style helps beginners who are learning modern TypeScript and JavaScript syntax. It proves that classic algorithms can still look clean and readable.
Program 5: Shell Sort for Already Sorted Data
This program applies Shell Sort to an already sorted array to show that it behaves correctly in best-case scenarios.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let sortedData: number[] = [1, 2, 3, 4, 5, 6, 7];
console.log("Sorted array:", shellSort(sortedData));This example reassures beginners that Shell Sort does not break when the input is already sorted. It is important for algorithms to handle all input cases gracefully.
Program 6: Shell Sort with Duplicate Values
This program demonstrates how Shell Sort handles repeated numbers.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let duplicates: number[] = [5, 3, 5, 2, 8, 3, 1];
console.log("Sorted array:", shellSort(duplicates));Shell Sort naturally handles duplicates without extra logic. Beginners can confidently use it for real-world data that often contains repeated values.
Program 7: Shell Sort for Large Numbers
This example sorts larger integer values using Shell Sort.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let largeNumbers: number[] = [500, 120, 750, 30, 900, 410];
console.log("Sorted array:", shellSort(largeNumbers));This program shows that Shell Sort works well for a wide range of numeric values. Beginners can apply it to many practical datasets.
Program 8: Shell Sort for Floating Point Numbers
Shell Sort can handle floating point numbers without any modification.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let decimals: number[] = [3.14, 1.41, 2.71, 0.99, 1.73];
console.log("Sorted array:", shellSort(decimals));This example highlights the flexibility of Shell Sort. It works smoothly for decimal values, making it useful in scientific or financial applications.
Program 9: Shell Sort Supporting Negative Numbers
Shell Sort already supports negative numbers, but this example clearly demonstrates that behavior.
function shellSort(arr: number[]): number[] {
let n = arr.length;
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < n; i++) {
let temp = arr[i];
let j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
return arr;
}
let mixedNumbers: number[] = [-12, 4, -7, 0, 9, -3, 5];
console.log("Sorted array:", shellSort(mixedNumbers));This program helps beginners see that no special changes are required for negative values. Understanding this builds confidence in using the algorithm.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about implementing Shell Sort in TypeScript.
Q1. Is Shell Sort better than Insertion Sort?
Shell Sort is generally faster because it reduces the number of shifts by sorting elements far apart first.
Q2. Is Shell Sort stable?
Shell Sort is not a stable sorting algorithm because equal elements may change their order.
Q3. Can Shell Sort handle large datasets?
It works well for medium-sized datasets but is not as fast as Merge Sort or Quick Sort for very large data.
Q4. Does Shell Sort work with negative numbers?
Yes, Shell Sort works naturally with negative and positive numbers.
Q5. Why is Shell Sort good for beginners?
It builds on Insertion Sort and helps beginners understand optimization concepts in a simple way.
Conclusion
Shell Sort is a smart and efficient improvement over basic sorting algorithms. In this article, we explored multiple TypeScript programs that implement Shell Sort using different approaches, loop styles, and data types. Each example showed how flexible and beginner-friendly this algorithm can be.
If you are learning TypeScript and sorting algorithms, practicing Shell Sort is a great step forward. Try modifying the gap values, changing the input data, and running the programs yourself. With practice, you will gain a deeper understanding and feel more confident exploring more advanced algorithms.




