2015-09-27 29 views
0

我是新來的Java編程,並試圖編寫一個函數,排序數組。我知道我的代碼對於這樣一個簡單的任務來說非常強大,但這是我考慮排序的第一種方式,所以我只是隨它而行。我的邏輯是採取一個數組,使所有0的長度相同的數組。然後使用for循環找到未排序數組的最小值,將其放在新數組的開始位置,然後用未排序數組的最大值替換未排序數組的最小值,直到整個數組全部爲最大值,I'已經填充了要排序的數組。我將手跟蹤下面幾個例子,因爲它是一個有點難以解釋:在Java中編寫數組的排序功能,並卡住

-Starting Array:[2, 3, 1, 4] 
    -1st Execution: [2, 3, 1, 4] [0, 0, 0, 0] 
    -2nd:   [2, 3, 4, 4] [1, 0, 0, 0] 
    -3rd:   [4, 3, 4, 4] [1, 2, 0, 0] 
    -4th:   [4, 4, 4, 4] [1, 2, 3, 0] 
    -5th:   [4, 4, 4, 4] [1, 2, 3, 4] 

我寫了返回數組的最小代碼,發現最小的指數,並返回數組的最大。我的目的是繼續這個過程並且保留for循環找到min的次數,然後當它等於數組長度-1時停止。我遇到了我的最後一個方法的問題 - sortMe - 因爲我的回報不會編譯和錯誤讀取:

Error: incompatible types 
found : int[] 
required: java.util.Arrays 

我已經附上所有我下面的代碼:

import java.util.Arrays; 

public class Homework4 { 
    public static void main (String[] args) { 
    int[] a = {20,2,5}; 
    System.out.println(Homework4.minArray(a)); 
    System.out.println(Homework4.maxArray(a)); 
    System.out.println(Homework4.minIndex(a)); 
    System.out.println(Arrays.toString(a)); 
    } 
    /* 1 This method will mimic the sort methond for Arrays 
    * It will be called sortMe and will take in an array and produce a sorted array 
    * In order to do this I will also create two methods: min and max 
    * The numbers in the array will be of type Int 
    * Homework4.sortMe([0 , 3, 4, 2, 1, 7]) -> [0 , 1, 2, 3, 4, 7] 
    * Homework4.sortMe([0]) -> [0] 
    * Homework4.sortMe([3, 8, 2, 14, 1)] -> [1, 2, 3, 8, 14] 
    * Template:*/ 
    public static int minArray (int[] x) {     //Produces the Minimum of an Array 
    int minVal = x[0]; 
    for (int i = 0; i < (x.length - 1); i = i + 1) { 
     if(x[i] < minVal) { 
     minVal = x[i];} 
    } 
    return minVal;} 
public static int minIndex (int[] x) {     //Returns the index of the Minimum 
    int minVal = x[0]; 
    int index = 0; 
    for (int i = 0; i < (x.length - 1); i = i + 1) { 
     if(x[i] < minVal) { 
     minVal = x[i]; 
     index = i;} 
    } 
    return index;} 

    public static int maxArray (int[] x) {     //Produces the Maximum of an Array 
    int maxVal = x[0]; 
    for (int i = 0; i < (x.length - 1); i = i + 1) { 
     if(x[i] > maxVal) { 
     maxVal = x[i];} 
    } 
    return maxVal;} 

    public static Arrays sortMe (int[] x) { //Sorts an Array 
    int[] sortedArray = new int [x.length]; 
    int minIterations = 0; 
     while (minIterations < x.length-1) { 
     for(int i = 0; i < x.length-1; i = i +1){ 
     sortedArray[i] = Homework4.minArray(x); 
     x[Homework4.minIndex(x)] = Homework4.maxArray(x); 
     minIterations++; 
     }} 
     return sortedArray; }  
} 

謝謝!

+2

指定發生錯誤的行。 – SanVed

+3

'公共靜態數組sortMe'你的方法說它返回類型'Arrays',但你試圖返回類型'int []'。 – csmckelvey

+0

請注意,這被稱爲「插入排序」。 – chrylis

回答

5

我說對了,你想返回一個整數數組嗎?我假設你已經將sortedArray聲明爲一個整型數組。

在這種情況下,該方法首標將需要返回類型是一個整數陣列,相同您已經使用一個整數數組作爲參數:

所以:

public static Arrays sortMe (int[] x) 

變爲:

public static int[] sortMe (int[] x) 
1
public static Arrays sortMe (int[] x) {...} 

應該是:

public static int[] sortMe (int[] x) {...} 
-1

將返回類型更改爲int []而不是Araays。

public static int[] sortMe (int[] x) { 
.. 
}