Java Program for ShellSort Last Updated : 10 Jun, 2022 Comments Improve Suggest changes Like Article Like Report In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h'th element is sorted. Java // Java implementation of ShellSort class ShellSort { /* An utility function to print array of size n*/ static void printArray(int arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } /* function to sort arr using shellSort */ int sort(int arr[]) { int n = arr.length; // Start with a big gap, then reduce the gap for (int gap = n / 2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already // in gapped order keep adding one more element // until the entire array is gap sorted for (int i = gap; i < n; i += 1) { // add a[i] to the elements that have been gap // sorted save a[i] in temp and make a hole at // position i int temp = arr[i]; // shift earlier gap-sorted elements up until // the correct location for a[i] is found int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; // put temp (the original a[i]) in its correct // location arr[j] = temp; } } return 0; } // Driver method public static void main(String args[]) { int arr[] = { 12, 34, 54, 2, 3 }; System.out.println("Array before sorting"); printArray(arr); ShellSort ob = new ShellSort(); ob.sort(arr); System.out.println("Array after sorting"); printArray(arr); } } /*This code is contributed by Rajat Mishra */ Output:Array before sorting 12 34 54 2 3 Array after sorting 2 3 12 34 54 Time Complexity: O(n2)Auxiliary Space: O(1) Please refer complete article on ShellSort for more details! Comment More infoAdvertise with us Next Article Java Program for ShellSort kartik Follow Improve Article Tags : Sorting Java Programs DSA Practice Tags : Sorting Similar Reads Java Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where first find the maximum element and place it at the end. We repeat the same process for the remaining element. Heap Sort in JavaBelow is the implementation of Heap Sort 3 min read Java Array Programs An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous 4 min read Java Program For Case Specific Sorting Given a string S consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase letters separately such that if the "i"th place in the original string had an Uppercase character then it should not have a lowercase character after being sorted and vice versa. it is desc 2 min read Java String Programs A String in Java is a sequence of characters that can be used to store and manipulate text data and It is basically an array of characters that are stored in a sequence of memory locations. All the strings in Java are immutable in nature, i.e. once the string is created we can't change it. This arti 4 min read Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically. 7 min read Java File Handling Programs Java is a programming language that can create applications that work with files. Files are containers that store data in different formats, such as text, images, videos, etc. Files can be created, read, updated, and deleted using Java. Java provides the File class from the java.io package to handle 3 min read Basic Calculator Program Using Java Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Example: Enter the numbers:Â 2Â 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0ApproachTake two numbers using the Scanner 2 min read How to Execute Native Shell Commands from Java Program? A shell command is a command that we can trigger using a keyboard and a command-line or a shell instead of a Graphical user interface. Usually, we would trigger shell commands manually. However, there can be instances where this needs to be done programmatically through Java. Java provides support t 5 min read Java Threading Programs - Basic to Advanced Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral 3 min read Java Regex Programs - Basic to Advanced In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. In Java Inte 5 min read Like