Java Program to Implement Shell Sort Algorithm

This is a java program to implement Shell Sort Algorithm.

Here is the source code of the Java Program to Implement Shell Sort. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a program to sort numbers using Shell Sort
  2. import java.util.Random;
  3.  
  4. public class Shell_Sort 
  5. {
  6.     public static int N = 20;
  7.     public static int[] sequence = new int[N];
  8.  
  9.     public static void shellSort() 
  10.     {
  11.         int increment = sequence.length / 2;
  12.         while (increment > 0) 
  13.         {
  14.             for (int i = increment; i < sequence.length; i++) 
  15.             {
  16.                 int j = i;
  17.                 int temp = sequence[i];
  18.                 while (j >= increment && sequence[j - increment] > temp) 
  19.                 {
  20.                     sequence[j] = sequence[j - increment];
  21.                     j = j - increment;
  22.                 }
  23.                 sequence[j] = temp;
  24.             }
  25.             if (increment == 2)
  26.                 increment = 1;
  27.             else
  28.                 increment *= (5.0 / 11);
  29.  
  30.         }
  31.     }
  32.  
  33.     static void printSequence(int[] sorted_sequence) 
  34.     {
  35.         for (int i = 0; i < sorted_sequence.length; i++)
  36.             System.out.print(sorted_sequence[i] + " ");
  37.     }
  38.  
  39.     public static void main(String args[]) 
  40.     {
  41.         System.out
  42.                 .println("Sorting of randomly generated numbers using SHELL SORT");
  43.         Random random = new Random();
  44.  
  45.         for (int i = 0; i < N; i++)
  46.             sequence[i] = Math.abs(random.nextInt(100));
  47.  
  48.         System.out.println("\nOriginal Sequence: ");
  49.         printSequence(sequence);
  50.  
  51.         System.out.println("\nSorted Sequence: ");
  52.         shellSort();
  53.         printSequence(sequence);
  54.     }
  55. }

Output:

$ javac Shell_Sort.java
$ java Shell_Sort
 
Sorting of randomly generated numbers using BUBBLE SORT
 
Original Sequence: 
67 57 55 13 83 80 29 89 30 46 68 71 6 12 5 3 68 8 18 6 
Sorted Sequence: 
3 5 6 6 8 12 13 18 29 30 46 55 57 67 68 68 71 80 83 89

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

👉 For weekly algorithms practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations