2015-09-27 37 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) { 
.. 
}