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:
How to Replace Many if Statements in Java
Java Program to Implement EnumMap API
Jackson JSON Views
Send email with SMTPS (eg. Google GMail)
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Control the Session with Spring Security
Introduction to Spring Data REST
Java Concurrency Interview Questions and Answers
Java Program to Check whether Graph is a Bipartite using DFS
Introduction to Spring MVC HandlerInterceptor
Jackson Annotation Examples
Java Program to Implement Sorted Doubly Linked List
Summing Numbers with Java Streams
Java Program to Implement Counting Sort
Truyền giá trị và tham chiếu trong java
Remove All Occurrences of a Specific Value from a List
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Implement the MD5 Algorithm
Hướng dẫn Java Design Pattern – Adapter
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Spring Boot - Admin Server
Iterating over Enum Values in Java
Cơ chế Upcasting và Downcasting trong java
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring Boot - Hystrix
JUnit5 Programmatic Extension Registration with @RegisterExtension
Spring REST API with Protocol Buffers
Spring AMQP in Reactive Applications
Creating Docker Images with Spring Boot
Introduction to Netflix Archaius with Spring Cloud
Java Program to Compare Binary and Sequential Search
How to Get the Last Element of a Stream in Java?