希尔排序是一种使用 java 编程语言对给定数字或数组进行排序的算法。它基于插入排序算法,根据要求对元素进行排序。它是使用分开的数字进行排序的元素,并比较彼此相距较远的元素。它是一种使用java语言按升序或降序设置元素的算法。它是一个元素中的划分数组,并使用 java.lang.String 将一个元素与另一个不同元素进行比较。这是一个比较两个距离较远的元素的排序过程。 希尔排序是插入排序方法的推广,用于排列数组元素。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
使用 java 的 shell 排序语法如下。
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; } }
描述:
public class Shell{ … }
int shellSort(int shell_array[]) { … }
int array_length = shell_array.length;
在两个元素之间留出间隙以对数组元素进行排序。
for (int elemnt_gap = array_length / 2; elemnt_gap > 0; elemnt_gap = elemnt_gap /2) { write shell sort algorithm here… }
将 shell 排序算法放在“for 循环”内。
该算法以表格形式排列数组元素。 较小的元素放在列的左侧,较大的元素放在列的右侧。
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;
创建main方法并返回排序元素。
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(); }
以下是不同的示例:
代码:
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] + " "); } }
输出:
代码:
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] + " "); } }
输出:
代码:
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] + " "); } }
输出:
描述:
以上是java中的希尔排序的详细内容。更多信息请关注PHP中文网其他相关文章!