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.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.