Home > Java > javaTutorial > body text

Shell sort in java

WBOY
Release: 2024-08-30 15:32:33
Original
306 people have browsed it

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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;
}
}
Copy after login

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{ … }
Copy after login
  • Create method for shell sorting with array variable.
int shellSort(int shell_array[]) { … }
Copy after login
  • Create array length for the given required array.
int array_length = shell_array.length;
Copy after login

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…
}
Copy after login

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;
Copy after login

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();
}
Copy after login

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] + " ");
}
}
Copy after login

Output:

Shell sort in java

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] + " ");
}
}
Copy after login

Output:

Shell sort in java

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] + " ");
}
}
Copy after login

Output:

Shell sort in java

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.

The above is the detailed content of Shell sort in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!