Java Program for Shellsort



In this article, we will learn to write a program for shell sort using Java. In the program, we'll apply this technique to sort an array and observe how the algorithm optimizes the sorting process by reducing the interval between elements.

Shell sort

Shell sort is a sorting technique that is similar to insertion sort, wherein elements are sorted which are at far ends (either end) of the array. This way, the interval size between the next and the second to last element reduces. This happens for all the elements in the array until the interval distance is reduced to 0.

Problem Statement

Write a Java program that implements Shell Sort to sort an array of integers. The program should follow the Shell Sort algorithm, repeatedly sorting elements at progressively smaller intervals until the entire array is sorted.

Input

my_arr = 12, 34, 54, 2, 3

Output

The array, after performing shell sort is :
2 3 12 34 54

Algorithm for ShellSort 

Below is the ShellSort algorithm ?

  • Determine the initial gap as half the length of the array.
  • For each gap, iterate through the array, picking each element and inserting it in the correct position within the gap-sorted array.
  • Repeat the process, halving the gap each time, until the gap becomes zero.
  • Return the sorted array.

Java program for ShellSort

Following is an example of ShellSort in Java ?

public class Demo {
   int shell_sort(int my_arr[]) {
      int arr_len = my_arr.length;
      for (int gap = arr_len / 2; gap > 0; gap /= 2) {
         for (int i = gap; i < arr_len; i += 1) {
            int temp = my_arr[i];
            int j;
            for (j = i; j >= gap && my_arr[j - gap] > temp; j -= gap)
            my_arr[j] = my_arr[j - gap];
            my_arr[j] = temp;
         }
      }
      return 0;
   }
   public static void main(String args[]) {
      int my_arr[] = { 12, 34, 54, 2, 3 };
      Demo my_instance = new Demo();
      my_instance.shell_sort(my_arr);
      System.out.println("The array, after performing shell sort is : ");
      int arr_len = my_arr.length;
      for (int i = 0; i < arr_len; ++i)
      System.out.print(my_arr[i] + " ");
      System.out.println();
   }
}

Output

The array, after performing shell sort is :
2 3 12 34 54

Time complexity: It varies between O(N) and O(N^2).

Space complexity: The worst-case space complexity is O(N) with O(1) auxiliary space.

Code explanation

This algorithm sorts the elements that are far apart from each other, thereby reducing the interval between those two elements. It can be understood as being a generalized version of insertion sort. Elements at specific intervals in an array are sorted first, and their interval distance reduces, thereby sorting all elements in the process.

When the first loop is iterated, the size of the array is taken and elements between size/2 are compared and swapped if they are not sorted. The same thing is repeated for all other elements. The elements are sorted by defining a temporary variable and swapping the elements.

In the second loop iteration, size/4 elements are compared and sorted. The same process goes on for the remaining elements, thereby sorting them. In the main function, the array is defined, and ?shell_sort' function is called by passing this array as a parameter.

Updated on: 2024-11-15T18:47:54+05:30

664 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements