Updated April 10, 2023
Definition of Shell sort in java
The shell sort is an algorithm to sort the given numbers or array using a java programming language. It is based on the insertion sort algorithm to sort elements as per requirement. It is a sort element using divided numbers and compares elements far from each other. It is an algorithm to set elements by ascending or descending order using java language. It is a divided array in an element and compares one element to another distinct element using java. It is a sorting procedure to compare two elements that are far away from each other. The Shell sort is a generalization of the insertion sort method to arranging array elements.
Syntax
The shell sort syntax using java is below.
int array_length = shell_array.length;
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2)
{
int j, i;
for ( i = elemnt_gap; i < array_length; i += 1)
{
int temprary_elemnt = shell_array[i];
for ( j = i; j >= elemnt_gap && shell_array[j - elemnt_gap] > temprary_elemnt; j -= elemnt_gap)
shell_array[j] = shell_array[j - elemnt_gap];
shell_array[j] = temprary_elemnt;
}
}
Description:
- The “array_length ” is the length of the given array for shell sorting.
- You can create a gap between two elements using the ” elemnt_gap ” variable.
- Use “for loop” to traverse the variable in the array element.
- The “temprary_elemnt” is used for sorting array elements.
- Then, you can start shell sorting in ascending order.
How does Shell sort work in Java?
- Create a primary class in java.
public class Shell{ … }
- Create method for shell sorting with array variable.
int shellSort(int shell_array[]) { … }
- Create array length for the given required array.
int array_length = shell_array.length;
Make a gap between two elements to sort array elements.
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) {
write shell sort algorithm here…
}
Place shell sorting algorithm inside of the “for loop.”
This algorithm arranges array elements in table format. The smaller element place on the left side of the column and the larger number is placed on the right side of the column.
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) {
int j, i;
for ( i = elemnt_gap; i < array_length; i += 1) {
int temprary_elemnt = shell_array[i];
int j;
for (j = i; j >= elemnt_gap && shell_array[j - elemnt_gap] > temprary_elemnt; j -= elemnt_gap)
shell_array[j] = shell_array[j - elemnt_gap];
shell_array[j] = temprary_elemnt;
}
}
return 0;
Create the main method and return the sorting element.
public static void main(String args[]) {
int shell_array[] = { 1, 4, 5, 2, 3 };
Shell shell = new Shell();
shell.shellSort(shell_array);
System.out.println("shell sort elements are: ");
int array_length = shell_array.length;
for (int i = 0; i < array_length; ++i)
System.out.print(shell_array[i] + " ");
System.out.println();
}
Examples
Below are the different examples:
Example #1: Single numerical values
Code:
import java.util.Arrays;
public class Shell {
int shellSort(int shell_array[]) {
int array_length = shell_array.length;
int j, i;
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) {
for (i = elemnt_gap; i < array_length; i += 1) {
int temprary_elemnt = shell_array[i];
for ( j = i; j >= elemnt_gap && shell_array[j - elemnt_gap] > temprary_elemnt; j -= elemnt_gap)
shell_array[j] = shell_array[j - elemnt_gap];
shell_array[j] = temprary_elemnt;
}
}
return 0;
}
public static void main(String args[]) {
int shell_array[] = { 8, 1, 4, 5, 2, 6, 3, 9, 7};
System.out.println("given array elements are : ");
System.out.println(Arrays.toString(shell_array));
Shell shell = new Shell();
shell.shellSort(shell_array);
System.out.println("shell sort elements are : ");
int array_length = shell_array.length;
for (int i = 0; i < array_length; ++i)
System.out.print(shell_array[i] + " ");
}
}
Output:
Example #2: Double numerical values
Code:
import java.util.Arrays;
public class Shell {
int shellSort(int shell_array[]) {
int array_length = shell_array.length;
int j, i;
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) {
for (i = elemnt_gap; i < array_length; i += 1) {
int temprary_elemnt = shell_array[i];
for ( j = i; j >= elemnt_gap && shell_array[j - elemnt_gap] > temprary_elemnt; j -= elemnt_gap)
shell_array[j] = shell_array[j - elemnt_gap];
shell_array[j] = temprary_elemnt;
}
}
return 0;
}
public static void main(String args[]) {
int shell_array[] = { 81, 17, 44, 58, 23, 69, 32, 90, 75};
System.out.println("given array elements are : ");
System.out.println(Arrays.toString(shell_array));
Shell shell = new Shell();
shell.shellSort(shell_array);
System.out.println("shell sort elements are : ");
int array_length = shell_array.length;
for (int i = 0; i < array_length; ++i)
System.out.print(shell_array[i] + " ");
}
}
Output:
Example #3: Multiple numerical values
Code:
import java.util.Arrays;
public class Shell {
int shellSort(int shell_array[]) {
int array_length = shell_array.length;
int j, i;
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) {
for (i = elemnt_gap; i < array_length; i += 1) {
int temprary_elemnt = shell_array[i];
for ( j = i; j >= elemnt_gap && shell_array[j - elemnt_gap] > temprary_elemnt; j -= elemnt_gap)
shell_array[j] = shell_array[j - elemnt_gap];
shell_array[j] = temprary_elemnt;
}
}
return 0;
}
public static void main(String args[]) {
int shell_array[] = { 888, 1, 44, 5573, 24, 6, 543, 901, 7000};
System.out.println("given array elements are : ");
System.out.println(Arrays.toString(shell_array));
Shell shell = new Shell();
shell.shellSort(shell_array);
System.out.println("shell sort elements are : ");
int array_length = shell_array.length;
for (int i = 0; i < array_length; ++i)
System.out.print(shell_array[i] + " ");
}
}
Output:
Description:
- You can see the given multiple types of numerical value.
- Shell sort creates a difference between elements.
- Then, sort the array in ascending order.
Conclusion
- The shell sort in java helps to arrange array elements as per the user’s requirement.
- It makes web applications sorted, simple, and understandable.
- The shell sort arranges data without the complexity and makes a user-friendly application.
Recommended Articles
This is a guide to Shell sort in java. Here we discuss definition, syntax, and parameters, working of the Shell sort in java examples with code implementation. You may also have a look at the following articles to learn more –