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:
Spring Boot - Service Components
Create a Custom Auto-Configuration with Spring Boot
Java – Combine Multiple Collections
Spring 5 Functional Bean Registration
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Jackson Unmarshalling JSON with Unknown Properties
Spring NoSuchBeanDefinitionException
How to Replace Many if Statements in Java
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement the Monoalphabetic Cypher
Java Program to Optimize Wire Length in Electrical Circuit
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Guide to Java 8’s Collectors
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Spring Boot - Exception Handling
Spring Boot - Creating Docker Image
Hướng dẫn Java Design Pattern – State
Java Program to implement Circular Buffer
Jackson – Marshall String to JsonNode
Java Program to Implement Cartesian Tree
Java Program to Construct K-D Tree for 2 Dimensional Data
Transactions with Spring and JPA
Spring Data JPA Delete and Relationships
A Guide to Iterator in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Guide to the Java TransferQueue
Count Occurrences of a Char in a String
A Guide to WatchService in Java NIO2
Java Program to Implement Stack using Two Queues