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 Streams vs Vavr Streams
Spring Webflux with Kotlin
Jackson – Change Name of Field
Spring RestTemplate Error Handling
Testing an OAuth Secured API with Spring MVC
Spring Cloud AWS – Messaging Support
Getting the Size of an Iterable in Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Compute Cross Product of Two Vectors
Dynamic Proxies in Java
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
JUnit 5 for Kotlin Developers
Java Program to Implement Circular Singly Linked List
Spring Boot - Enabling Swagger2
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Custom JUnit 4 Test Runners
Examine the internal DNS cache
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to find the number of occurrences of a given number using Binary Search approach
What is a POJO Class?
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Find Nearest Neighbor Using Linear Search
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring MVC Tutorial
Working with Kotlin and JPA
Guava Collections Cookbook
Template Engines for Spring
Java Program to Perform Polygon Containment Test
Refactoring Design Pattern với tính năng mới trong Java 8
Phương thức forEach() trong java 8