希爾排序是一種使用 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中文網其他相關文章!