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.
//This is a program to sort numbers using Shell Sort
import java.util.Random;
public class Shell_Sort
{
public static int N = 20;
public static int[] sequence = new int[N];
public static void shellSort()
{
int increment = sequence.length / 2;
while (increment > 0)
{
for (int i = increment; i < sequence.length; i++)
{
int j = i;
int temp = sequence[i];
while (j >= increment && sequence[j - increment] > temp)
{
sequence[j] = sequence[j - increment];
j = j - increment;
}
sequence[j] = temp;
}
if (increment == 2)
increment = 1;
else
increment *= (5.0 / 11);
}
}
static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
public static void main(String args[])
{
System.out
.println("Sorting of randomly generated numbers using SHELL SORT");
Random random = new Random();
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(100));
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
shellSort();
printSequence(sequence);
}
}
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
Related posts:
Converting between an Array and a List in Java
Java String Conversions
Spring Boot - Admin Client
Java Deep Learning Essentials - Yusuke Sugomori
Auditing with JPA, Hibernate, and Spring Data JPA
How to use the Spring FactoryBean?
Java Program to Search for an Element in a Binary Search Tree
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
LinkedList trong java
Java Program to Implement Fenwick Tree
Java Program to Perform Matrix Multiplication
Java Program to Implement Sieve Of Atkin
HTTP Authentification and CGI/Servlet
Java Program to Implement Hamiltonian Cycle Algorithm
Java 14 Record Keyword
Comparing Dates in Java
Setting a Request Timeout for a Spring REST API
Introduction to Project Reactor Bus
Java Program to Generate Date Between Given Range
OAuth2 Remember Me with Refresh Token
Java Program to Check whether Directed Graph is Connected using BFS
HttpAsyncClient Tutorial
Guide to Dynamic Tests in Junit 5
Guava Collections Cookbook
Optional trong Java 8
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Reversing a Linked List in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to Implement Bucket Sort
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java 9 Stream API Improvements
Spring Boot: Customize Whitelabel Error Page