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:
Java Program to Implement Maximum Length Chain of Pairs
A Quick Guide to Spring MVC Matrix Variables
@Order in Spring
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Immutable Objects in Java
Introduction to the Java NIO2 File API
Java Program to Perform the Shaker Sort
Java Program to Implement Solovay Strassen Primality Test Algorithm
Spring @RequestMapping New Shortcut Annotations
An Example of Load Balancing with Zuul and Eureka
Java Program to Implement Bellman-Ford Algorithm
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Getting Started with GraphQL and Spring Boot
Introduction to Project Reactor Bus
Constructor Injection in Spring with Lombok
How to Read HTTP Headers in Spring REST Controllers
Java Program to Check whether Directed Graph is Connected using DFS
Hướng dẫn sử dụng Java Annotation
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Program to Generate Date Between Given Range
Mapping Nested Values with Jackson
Quick Intro to Spring Cloud Configuration
Iterating over Enum Values in Java
Java Program to Perform Partial Key Search in a K-D Tree
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement Sparse Matrix
Java Concurrency Interview Questions and Answers
Hướng dẫn Java Design Pattern – Flyweight
Assert an Exception is Thrown in JUnit 4 and 5
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement Heap
Spring Boot - Internationalization