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:
Request Method Not Supported (405) in Spring
Ways to Iterate Over a List in Java
Explain about URL and HTTPS protocol
How to Implement Caching using Adonis.js 5
Creating Docker Images with Spring Boot
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Describe the Representation of Graph using Incidence List
Guide to CopyOnWriteArrayList
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Guide to UUID in Java
Removing all Nulls from a List in Java
Different Ways to Capture Java Heap Dumps
Spring Security Form Login
LinkedHashSet trong java
Java Program to Implement the One Time Pad Algorithm
Object Type Casting in Java
Java Program to Implement Gabow Algorithm
Using the Not Operator in If Conditions in Java
Number Formatting in Java
Convert XML to JSON Using Jackson
Spring Boot Application as a Service
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement Expression Tree
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Lấy ngày giờ hiện tại trong Java
Java Program to Implement Quick Sort Using Randomization
Converting a Stack Trace to a String in Java
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Suffix Array
Java Program to Use rand and srand Functions
Spring Boot - Bootstrapping