[英]Using Insertion Sort to sort an array
我應該采用一組數字:{51、63、48、98、75、63、92、30、32、32、36、89、4、76、73、90、64、99、36 ,96},然后從最低到最高,然后從最高到最低進行排序。
當我嘗試從最高到最低打印時,它會使第一個輸出相同。 有人在我的代碼中看到任何錯誤嗎?
package l7c14sort;
import java.util.Arrays;
public class L7C14Sort {
public static void main(String a[]){
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
System.out.println("Original input: "+Arrays.toString(arr1)+"\n");
System.out.println("Lowest to highest:\n");
for(int i:arr2)
{
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
for(int k:arr3)
{
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
public static int[] doInsertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
return input;
}
public static int[] doInsertionSortAgain(int[] input2){
int temp2;
for (int k = 1; k < input2.length; k++) {
for(int j = k ; j > 0 ; j--){
if(input2[j] > input2[j-1]){
temp2 = input2[j];
input2[j] = input2[j-1];
input2[j-1] = temp2;
}
}
}
return input2;
}
}
輸出:
Original input: [99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51,
48, 36, 36, 32, 32, 30, 4]
最高到最低:
99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51, 48, 36, 36, 32, 32, 30, 4,
最低到最高:
4,30,32,32,36,36,48,51,63,63,64,73,75,76,89,90,92,96,98,99
好消息:您的算法運行良好。
在Java中,數組是按引用而不是按值傳遞的。 這意味着您設置int[] arr2 = doInsertionSort(arr1);
,則將數組arr2
設置為doInsertionSort
方法的結果,該方法對input
參數進行排序后將其返回。 基本上, arr1
, arr2
, arr3
, input
和input2
都指向同一數組。
您有兩個簡單的選項可解決您正在打印的事實:
重組main()
以便使用一個數組:打印其內容,將其從最低到最高排序,再次打印,將其從最高到最低排序,然后再次打印。 (如果這是課程的話,這可能是您的老師打算為您做的。)
復制input
參數以進行操作。 您可以使用System.arraycopy()
進行此操作,如下所示:
int[] myArray; System.arraycopy(input, 0, myArray, 0, input.length );
然后,對於第二種情況,你需要編輯您的使用方法myArray
,而不是input
您所使用每個其他時間input
。
需要注意的是,您不需要調用變量input2
, temp2
等。就像i
, j
和k
超出范圍並在for
循環結束后被忘記一樣,變量input
和temp
在范圍外temp
意義。您聲明它們的塊。
希望這可以幫助!
由於數組是可變的,因此最終結果相同。 由於下面的代碼,輸入數組被突變並打印其最終值。(從最高到最低)。
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
如果您按照以下方式組織代碼:
public static void main(String a[]) {
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
System.out.println("Original input: " + Arrays.toString(arr1) + "\n");
System.out.println("Lowest to highest:\n");
int[] arr2 = doInsertionSort(arr1);
for (int i : arr2) {
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
int[] arr3 = doInsertionSortAgain(arr1);
for (int k : arr3) {
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
您將獲得:
原始輸入:[51、63、48、98、75、63、92、30、32、32、36、89、4、76、73、90、64、99、36、96]
最低至最高:4、30、32、32、36、36、48、51、63、63、64、73、75、76、89、90、92、96、98、99,
最高到最低:99、98、96、92、90、89、76、75、73、64、63、63、51、48、36、36、32、32、30、4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.