R Program to Implement Shell Sort

R Program to Implement Shell Sort

Shell Sort is an improved version of the simple Insertion Sort algorithm. It works by comparing elements that are far apart first and then slowly reducing the gap between elements. This idea helps move values closer to their correct position much faster than normal insertion sort. For beginners learning R programming, Shell Sort is a great example of how a small change in approach can greatly improve performance.

Learn C Programming For Free on Windows

A beginner-friendly course that teaches real C programming using a Windows compiler. Learn arrays, pointers, functions, and file handling step by step with practical lessons.

Start Learning C Programming

Shell Sort is useful in real-world applications where data sets are medium-sized and need to be sorted faster than basic algorithms like Bubble Sort or Insertion Sort. It is often used in teaching because it builds directly on ideas beginners already know, such as loops and comparisons, while gently introducing more advanced thinking. Learning Shell Sort helps you understand why algorithm design matters and how traditional ideas are refined over time.

Program 1: Basic Shell Sort using loops

This first program shows a clean and traditional implementation of Shell Sort in R using simple loops. It uses predefined integer data so beginners can run it and see the output immediately.

shell_sort <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(45, 23, 53, 12, 9, 34)
print(shell_sort(data))

This program starts with a large gap and reduces it step by step until it becomes one. By doing this, elements move faster to their correct positions. Beginners can understand this as sorting from far apart first, then doing fine adjustments at the end.

Program 2: Shell Sort with custom gap sequence

This version allows you to define your own gap sequence. It helps beginners see how gap choices affect sorting behavior.

shell_sort_custom_gap <- function(arr, gaps) {

  n <- length(arr)

  for (gap in gaps) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

  }

  return(arr)

}

data <- c(64, 34, 25, 12, 22, 11)
gaps <- c(3, 1)

print(shell_sort_custom_gap(data, gaps))

This approach is useful because Shell Sort does not have a single fixed gap rule. Beginners learn that algorithms can be flexible and still correct. It also encourages experimenting with values to see how results change.

Program 3: Shell Sort written step by step for clarity

This program focuses on readability rather than compact code. It follows a very traditional style that values clarity and learning over clever tricks.

shell_sort_clear <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    i <- gap + 1

    while (i <= n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp
      i <- i + 1

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(19, 2, 31, 45, 6, 11)
print(shell_sort_clear(data))

This version is ideal for beginners who want to follow the flow of the algorithm slowly. Each step is easy to trace, making it a good choice for learning and debugging.

Program 4: Shell Sort using a for-loop driven gap reduction

This example uses a different style to reduce the gap using a for loop. It shows that the same logic can be written in multiple valid ways.

shell_sort_for_gap <- function(arr) {

  n <- length(arr)

  for (gap in floor(n / 2):1) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

  }

  return(arr)

}

data <- c(88, 33, 55, 11, 44)
print(shell_sort_for_gap(data))

This program helps beginners see that there is no single “correct” coding style. What matters is that the logic is sound and easy to understand. This builds confidence when writing R programs.

Program 5: Shell Sort using helper functions

This version separates comparison logic into a helper function. It follows a traditional programming idea of dividing work into small parts.

insert_gap <- function(arr, i, gap) {

  temp <- arr[i]
  j <- i

  while (j > gap && arr[j - gap] > temp) {
    arr[j] <- arr[j - gap]
    j <- j - gap
  }

  arr[j] <- temp

  return(arr)

}

shell_sort_helper <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    for (i in (gap + 1):n) {
      arr <- insert_gap(arr, i, gap)
    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(27, 14, 39, 10, 33)
print(shell_sort_helper(data))

By using helper functions, beginners learn how large problems can be broken into smaller, reusable pieces. This is a traditional and valuable programming habit.

Program 6: Shell Sort with while loops only

This program avoids for loops and uses only while loops. It is useful for learners who want more practice with loop control.

shell_sort_while <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    i <- gap + 1

    while (i <= n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp
      i <- i + 1

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(50, 20, 40, 10, 30)
print(shell_sort_while(data))

This version reinforces the idea that logic is independent of syntax choice. Beginners gain flexibility and stronger control over program flow.

Program 7: Shell Sort for already nearly sorted data

This program demonstrates how Shell Sort performs well when data is almost sorted.

shell_sort_near_sorted <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(1, 2, 4, 3, 5, 6)
print(shell_sort_near_sorted(data))

Shell Sort shines in situations like this. Beginners can learn why it is often preferred over simpler algorithms when data is partially ordered.

Program 8: Shell Sort with input validation

This example adds simple checks to make the function safer to use.

shell_sort_safe <- function(arr) {

  if (!is.numeric(arr)) {
    stop("Input must be numeric")
  }

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(9, 4, 7, 1, 3)
print(shell_sort_safe(data))

This teaches beginners that writing good programs also means handling wrong input gracefully. It is an important habit in real-world programming.

Program 9: Shell Sort handling negative and floating-point numbers

Shell Sort naturally supports negative and decimal values, but this final example shows it clearly using mixed data.

shell_sort_extended <- function(arr) {

  n <- length(arr)
  gap <- floor(n / 2)

  while (gap > 0) {

    for (i in (gap + 1):n) {

      temp <- arr[i]
      j <- i

      while (j > gap && arr[j - gap] > temp) {
        arr[j] <- arr[j - gap]
        j <- j - gap
      }

      arr[j] <- temp

    }

    gap <- floor(gap / 2)

  }

  return(arr)

}

data <- c(-4.5, 2.1, -1.3, 3.7, 0.0)
print(shell_sort_extended(data))

This program confirms that Shell Sort works well with real-world numeric data. Beginners can confidently use it without worrying about value types.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Shell Sort in R in a simple and friendly way.

Q1. Why is Shell Sort faster than Insertion Sort?
Shell Sort moves elements over long distances early, reducing the total number of shifts needed later.

Q2. Is Shell Sort stable?
Shell Sort is not stable by default because elements may jump over equal values during sorting.

Q3. When should I use Shell Sort in R?
Shell Sort is good for medium-sized datasets when you want better performance than simple sorting algorithms.

Q4. Does Shell Sort work with decimals and negatives?
Yes, Shell Sort handles both without any special changes.

Conclusion

Shell Sort is a smart improvement over basic sorting techniques and is a great algorithm for beginners learning R. It builds on familiar ideas like insertion and loops while introducing the powerful concept of gap-based sorting.

By practicing these different R programs, you can better understand how Shell Sort works and when to use it. Keep experimenting with data, try different gap values, and enjoy exploring classic algorithms that have stood the test of time.

Scroll to Top