排序数组类不起作用

时间:2013-11-28 12:10:15

标签: java arrays class sorting methods

我编写了一个应该创建随机数组的类,并对其进行排序。我为每个方法编写了方法,每个方法中的代码都是独立工作的。但是当我一起使用它们时,我无法使用sort方法对随机方法创建的内容进行排序。有什么想法吗?

注意,如果有帮助,我使用Java进行编码并使用NetBeans。而且我不想要更简单的代码编写方法,我只是想要帮助它使其工作。

这是我的代码。

public class SortUtility {

    private int[] num;
    static Random rand = new Random();
    private int c = 0;
    private int swap = 0;
    private int compare = 0;
    private int p = 10;
    private int[] b;
    //private int[] ex;

    public SortUtility() {
        num = new int[p];

    }

    public SortUtility(int[] Startnum) {
        int[] b = Startnum;
    }

    public int[] createRandomArray(int max, int min, int[] num) {
        //private int [10] ex;
        int [] ex = new int[num.length];

        for (int i = 0; i < num.length; i++) {
            num[i] = rand.nextInt(max + 1 - min) + min;
        }

        for (int i = 0; i < num.length; i++) {
            ex[i] = num[i];
        }

        return ex;
    }

    public int[] sortArray1(int[] ex) {
        int a;
        printItems();

        do {
            c++;
            for (a = 0; a < num.length - 1; a++) {
                compare++;
                if (num[a] > num[a + 1]) {
                    int temp = num[a];
                    num[a] = num[a + 1];
                    num[a + 1] = temp;
                    swap++;
                    System.out.println(num[2]);
                }
            }
            printItems();
        } while (c != num.length);

        return num;
    }

    public void printItems() {
        System.out.print("\nPass " + c + " Compares " + compare + " Swaps " + swap + " " + "items ");
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + " ");
        }
    }

    public int[] sortArray2(int[] ex) {
        int a;
        int swap2 = 0;
        printItems();

        do {
            c++;
            for (a = 0; a < num.length - 1 - (c - 1); a++) {
                compare++;
                if (num[a] > num[a + 1]) {
                    int temp = num[a];
                    num[a] = num[a + 1];
                    num[a + 1] = temp;
                    swap++;
                }
            }
            if (swap2 < swap && swap2 != swap) {
                if (swap2 < swap) {
                    swap2 = swap;
                }
            }
            swap2++;
            printItems();
        } while (swap + 2 != swap2);

        return num;
    }
}

这是我的客户

package sortclient;

public class SortClient {

    public static void main(String[] args) {
        int[] num = {25, 7, 99, 14, 55, 3, 47, 6, 1};
        SortUtility ds = new SortUtility();
        //SortUtility j = new SortUtility(l);
        SortUtility ab = new SortUtility();
        int[] a = ab.createRandomArray(99, 0, num);

        for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
    ab.sortArray1(a);

    }
}

以下是我运行客户端时的结果:

run:
52
8
82
87
93
29
94
33
46

Pass 0 Compares 0 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 1 Compares 9 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 2 Compares 18 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 3 Compares 27 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 4 Compares 36 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 5 Compares 45 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 6 Compares 54 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 7 Compares 63 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 8 Compares 72 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 9 Compares 81 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 
Pass 10 Compares 90 Swaps 0 items 0 0 0 0 0 0 0 0 0 0 BUILD SUCCESSFUL (total time: 0     seconds)

证明随机数组方法有效并且排序类型有效

3 个答案:

答案 0 :(得分:0)

您编写的代码是错误的。首先改变

     public SortUtility(int[] Startnum) {
        int[] b = Startnum;
    }

to:
    public SortUtility(int[] Startnum) {
    num = Startnum;
    }

and then

将createRandomArray的方法签名更改为:

    public int[] createRandomArray(int max, int min) 

createRandomArray中的更改是必需的,因为您在不同对象中填充数组并尝试对不同的数组对象进行排序。 由于您为构造函数本地对象分配了无用的数组空间,因此保证构造函数的更改。

使用以下修改后的代码供您参考。

     import java.util.Random;


public class SortClient {

    public static void main(String[] args) {
        int[] num = {25, 7, 99, 14, 55, 3, 47, 6, 1};
        SortUtility ds = new SortUtility();
        //SortUtility j = new SortUtility(l);
        SortUtility ab = new SortUtility(num);
        int[] a = ab.createRandomArray(99, 0);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        ab.sortArray1(a);




    }
}

 class SortUtility {

private int[] num;
static Random rand = new Random();
private int c = 0;
private int swap = 0;
private int compare = 0;
private int p = 10;
private int[] b;
//private int[] ex;

public SortUtility() {
    num = new int[p];

}

public SortUtility(int[] Startnum) {
    num = Startnum;
}

public int[] createRandomArray(int max, int min) {
    //private int [10] ex;
    int [] ex = new int[num.length];
    for (int i = 0; i < num.length; i++) {
        num[i] = rand.nextInt(max + 1 - min) + min;

    }
    for (int i = 0; i < num.length; i++) {
        ex[i] = num[i];
    }
    return ex;
}

public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
}

public void printItems() {
    System.out.print("\nPass " + c + " Compares " + compare + " Swaps " + swap + " " + "items ");
    for (int i = 0; i < num.length; i++) {
        System.out.print(num[i] + " ");
    }
}

public int[] sortArray2(int[] ex) {
    int a;
    int swap2 = 0;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1 - (c - 1); a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
            }
        }
        if (swap2 < swap && swap2 != swap) {
            if (swap2 < swap) {
                swap2 = swap;
            }
        }
        swap2++;
        printItems();
    } while (swap + 2 != swap2);
    return num;
}
}

答案 1 :(得分:0)

在您的类SortUtility中,有一个名为“sortArray1”的方法。请注意,它接收名为“ex”的数组作为参数。另外,请注意它对“num”数组执行排序(num用0初始化)。所以,你的方法正在运行,但是排序是在一个0的数组上完成的,而不是你刚收到的数组作为参数。

因此,为了使它们正确地协同工作,在上述方法中将引用从“num”数组更改为“ex”数组。

最后,上述方法在变更之后是:

public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < ex.length - 1; a++) {
            compare++;
            if (ex[a] > ex[a + 1]) {
                int temp = ex[a];
                ex[a] = ex[a + 1];
                ex[a + 1] = temp;
                swap++;
                System.out.println(ex[2]);
            }
        }
        printItems();
    } while (c != ex.length);
    return ex;
}

请查看更改是否会对您的逻辑产生任何副作用。

P.s。:你正在使用缓慢的冒泡排序。考虑使用合并排序,堆排序和快速排序。检查此网站http://www.sorting-algorithms.com/

答案 2 :(得分:0)

从第一眼看,您在SortUtility中的方法

 public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
}  
没有使用提供的数组参数 ex ,而您正在对内部数组进行排序(这是您保留它的另一个问题,然后尝试对不同的传递数组进行排序)。快速解决方法是在排序循环之前添加
 public int[] sortArray1(int[] ex) {
    int a;
    printItems();
    do {
        c++;
        for (a = 0; a < num.length - 1; a++) {
            compare++;
            if (num[a] > num[a + 1]) {
                int temp = num[a];
                num[a] = num[a + 1];
                num[a + 1] = temp;
                swap++;
                System.out.println(num[2]);
            }
        }
        printItems();
    } while (c != num.length);
    return num;
} 
。请注意,您传递的原始数组将被排序(因为它将指向相同的内部指示对象),因此如果您想保留它,请将
  num = ex;  
替换为
  num = ex;