检查数组的选择排序方法

时间:2014-01-14 13:43:49

标签: java arrays sorting recursion

我有这个方法,我从一个关于选择排序的网站,我需要检查它的工作原理:

public static void selectionSort(int[] data, int low, int high) {
    if (low < high) {
    swap(data, low, findMinIndex(data, low));
    selectionSort(data, low + 1, high);

    }


public static void swap(int[] array, int index1, int index2) {
    int tmp = array[index1];
    array[index1] = array[index2];
    array[index2] = tmp;
    }

    public static int findMinIndex(int[] data, int index) {
        int minIndex;
        if (index == data.length - 1)
        return index;
        minIndex = findMinIndex(data, index + 1);
        if (data[minIndex] < data[index])
        return minIndex;
        else
        return index;
        }



public static void main (String[] args) {

int[] numbers = {3, 15, 1, 9, 6, 12, 21, 17, 8}; }

我的问题是如何在主程序中运行程序?(运行程序的代码是什么) 感谢。

1 个答案:

答案 0 :(得分:2)

假设这一切都在一个类中:

public static void main (String[] args) {
  int[] numbers = {3, 15, 1, 9, 6, 12, 21, 17, 8};
  selectionSort(numbers, 0, 8);
  System.out.println(Arrays.toString(numbers));
}