简体   繁体   English

作业:排序算法

[英]Homework: Sorting Algorithms

I am supposed to create an algorithm that sorts according to these steps:我应该创建一个根据以下步骤排序的算法:

Method 1方法一

Select the lowest number in the list and swap with the first number.选择列表中最小的数字并与第一个数字交换。

Select the lowest number in the list and swap with the second number.选择列表中最小的数字并与第二个数字交换。 Start checking from the second number.从第二个数字开始检查。

Select the lowest number in the list and swap with the third number.选择列表中最小的数字并与第三个数字交换。 Start checking from the third number.从第三个数字开始检查。

Select the lowest number in the list and swap with the fourth number.选择列表中最小的数字并与第四个数字交换。 Start checking from the fourth number.从第四个数字开始检查。

Repeat…until you reach the last number.重复……直到你到达最后一个数字。

Currently, this is the code I have come up with:目前,这是我想出的代码:

public static void method1() {
    int low = 999;        
    int index = 0;
    int safe;
    int[] num = new int[] { 33, 22, 8, 59, 14, 47, 60, 27 };

    for(int i = 0; i < num.length; i++) {
        if(low > num[i]) {
            low = num[i];
            index = i;
        }
    }

    for (int i = 0; i < num.length; i++) {
        safe = num[i];
        num[i] = num[index];
        low = 999;
        for(int j = (i+1); j < num.length; j++) {
            if(low > num[j]) {
                low = num[j];
            }
        }
    }

    for (int i = 0; i < num.length; i++) {
        System.out.print(num[i] +", ");
    }
}

The output looks like this:输出如下所示:

run:
8, 8, 8, 8, 8, 8, 8, 8, 
BUILD SUCCESSFUL (total time: 0 seconds)

Why am I only getting values of 8 in the output?为什么我在输出中只得到 8 的值? As this is homework, please don't tell me the answer.由于这是作业,请不要告诉我答案。 I would only like guidance, thanks!只求指导,谢谢!

EDIT:编辑:

Code now looks like this:代码现在看起来像这样:

int low = 999;        
        int index = 0;
        int safe;
        int[] num = new int[] { 33, 22, 8, 59, 14, 47, 60, 27 };

        for(int i = 0; i < num.length; i++) {
            if(low > num[i]){
                low = num[i];
                index = i;
            }
        }

        for (int i = 0; i < num.length; i++) {
            safe = num[i];
            num[i] = num[index];
            low = 999;
            for(int j = (i+1); j < num.length; j++) {
                if(low > num[j]){
                    low = num[j];
                    index = j;
                }
            }
        }

        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] +", ");
        }
        System.out.println("");

Which gives me an output of:这给了我一个输出:

run:
8, 8, 8, 14, 14, 27, 27, 27, 
BUILD SUCCESSFUL (total time: 0 seconds)

The date for this homework has been and gone, but thought I would add some step-by-step methodology.这个作业的日期已经过去了,但我想我会添加一些循序渐进的方法。

The way I would approach this is to break it down into small steps.我解决这个问题的方法是将其分解为小步骤。 Each step should be a method or function.每个步骤都应该是一个方法或函数。


1. The first step is to find the smallest number in the array, starting from N. 1.第一步是从N开始,在数组中找到最小的数。

So the method for this would be:所以这个方法是:

private int findLowestStartingAtNth( int n ) {
    int lowest = Integer.MAX_VALUE;
    for( int i = n ; i < numbers.length ; i++ ) {
        if( numbers[i] < lowest ) {
            lowest = numbers[i]; 
        }
    }
    return lowest;
}

2. Then we need to swap two arbitrary numbers in an array. 2. 然后我们需要交换一个数组中的两个任意数字。

This is quite simple:这很简单:

private void swapNumbers( int i, int j ) {
    int temp = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = temp;
}

3. But if we want the output of findLowestStartingAtNth() to feed into the input of swapNumbers() , then we need to return the index not the number itself. 3. 但是如果我们希望findLowestStartingAtNth()的输出输入到swapNumbers()的输入中,那么我们需要返回索引而不是数字本身。

So the method from step 1. is altered to be:因此,步骤 1. 中的方法更改为:

private int findLowestStartingAtNth( int n ) {
    int lowest = Integer.MAX_VALUE;
    int index = n;
    for( int i = n ; i < numbers.length ; i++ ) {
        if( numbers[i] < lowest ) {
            lowest = numbers[i]; 
            index = i;
        }
    }
    return index;
}

4. Let's use what we have to achieve step one 4.让我们用我们所拥有的来实现第一步

Select the lowest number in the list and swap with the first number.选择列表中最小的数字并与第一个数字交换。

The first number is the zero-th in the array.第一个数字是数组中的第零个数字。

int numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
int found = findLowestStartingAtNth( 0 );
swapNumbers(0, found);

5. We have a pattern. 5. 我们有一个模式。 Start checking from 1st, swap with 1st.从第一个开始检查,与第一个交换。 Start checking from 2nd, swap with 2nd.从 2nd 开始检查,与 2nd 交换。 Start checking with X, swap with X.用 X 开始检查,用 X 交换。

So let's wrap this pattern in a further method:因此,让我们将这个模式包装在另一个方法中:

private int[] numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
private void sort() {
    for( int i = 0 ; i < numbers.length ; i++ ) {
        int j = findLowestStartingAtNth( i );
        swapNumbers(i, j);
    }
}

6. Finally, wrap it in a class, and trigger it from main() method. 6. 最后,将其包装在一个类中,并从main()方法中触发它。 See how clear the code is because it is broken into small steps.看看代码有多清晰,因为它被分成小步骤。

The entire resulting class would be:整个结果类将是:

public class Method1 {

    public static void main(String[] args) {
        Method1 m = new Method1();
        m.numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
        m.sort();
        System.out.println(Arrays.toString(m.numbers));
    }

    private int[] numbers;

    private int findLowestStartingAtNth( int n ) {
        int lowest = Integer.MAX_VALUE;
        int index = n;
        for( int i = n ; i < numbers.length ; i++ ) {
            if( numbers[i] < lowest ) {
                lowest = numbers[i]; 
                index = i;
            }
        }
        return index;
    }

    private void swapNumbers( int i, int j ) {
        int temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;
    }

    private void sort() {
        for( int i = 0 ; i < numbers.length ; i++ ) {
            int j = findLowestStartingAtNth( i );
            swapNumbers(i, j);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM