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
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:
public class Shell{ … }
int shellSort(int shell_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(); }
Below are the different examples:
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:
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:
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:
The above is the detailed content of Shell sort in java. For more information, please follow other related articles on the PHP Chinese website!