简体   繁体   English

查找数组中第三小的索引

[英]Find the third smallest index in array

I'm trying to find the indexes of the three smallest elements in an array.我试图找到数组中三个最小元素的索引。 My comments are in Norwegian, I can translate them if that's necessary!我的评论是挪威语,如果有必要,我可以翻译它们!

It's an assignment, and I have to do the following: - return an array with the indexes of the three smallest values - have three help variables for index, and three for their values - use the method "indekssortering" to give the indexes a start value这是一个赋值,我必须执行以下操作: - 返回一个包含三个最小值的索引的数组 - 有三个用于索引的帮助变量,三个用于它们的值 - 使用“indekssortering”方法为索引提供一个开始价值

This method is trying to find the indexes.此方法正在尝试查找索引。 I have m1, m2 and m3 as start-indexes, but they will be changed in the for-loop if we find a smaller number.我有 m1、m2 和 m3 作为起始索引,但如果我们找到较小的数字,它们将在 for 循环中更改。 m1_verdi, m2_verdi and m3_verdi is the values. m1_verdi、m2_verdi 和 m3_verdi 是值。 They base themselves on the indexes from m1, m2 and m3.它们基于来自 m1、m2 和 m3 的索引。

//oppgave 9
    public static int[] tredjeMin(int[] a){
        if (a.length < 3) {
            throw new java.util.NoSuchElementException("Arrayet har lengden " + a.length + ", skal ha lengde  >= 3!");
        }

        int[] tre = Arrays.copyOfRange(a, 0, 3); //Kopierer de tre forste tallene i arrayet a
        int[] in = indekssortering(tre); //Indekssorterer de tre forste tallene

        int m1 = in[0]; // STARTVERDI: index til minste verdi
        int m2 = in[1]; // STARTVERDI: index til nest minste verdi
        int m3 = in[2]; // STARTVERDI: index til nest, nest minste verdi

        int m1_verdi = a[m1]; //  STARTVERDI: minste verdi
        int m2_verdi = a[m2]; // STARTVERDI: nest minste verdi
        int m3_verdi = a[m3]; // STARTVERDI: nest, nest minste verdi

        for (int i = 0; i < a.length; i++) { // Looper gjennom arrayet a
            if (a[i] < m3_verdi) {
                if (a[i] < m2_verdi) {
                    if (a[i] < m1_verdi) {
                        m2 = m1;
                        m2_verdi = m1_verdi; // Ny nest minst

                        m1 = i;
                        m1_verdi = a[m1]; // Ny minst
                    } else {
                        m3 = m2;
                        m3_verdi = m2_verdi; // ny nest, nest minst

                        m2 = i;
                        m2_verdi = a[m2]; // Ny nest minst
                    }
                } else {
                    m3 = i;
                    m3_verdi = a[m3]; // Ny nest, nest minst
                }
            }
        }

        return new int[] {m1, m2, m3};
    }

The method is calling the method "indekssortering" (index sorting), which is this one:该方法正在调用方法“indekssortering”(索引排序),就是这个:

//oppgave 8
    public static int [] indekssortering(int[] a){
        int[] indeks = new int[a.length]; //Oppretter tomt array med samme lengde som a
        int[] kopi = Arrays.copyOf(a, a.length); // Oppretter kopi av a
        for (int i = 0; i < kopi.length; i++) { // Bubble sort av kopi:
            for (int j = 0; j < kopi.length-1; j++) {
                if (kopi[j] > kopi[j+1]) {
                    int temp = kopi[j];
                    kopi[j] = kopi[j+1];
                    kopi[j+1] = temp;
                }
            }
        }

        int i = 0;
        while (i < kopi.length) {
            for (int j = 0; j < kopi.length; j++) { // Per tall i sortert kopi, loop gjennom hele arrayet
                if (kopi[i] == a[j]) { // Sjekker om valgt tall matcher et tall fra original arrayet a
                    indeks[i] = j; // Setter indeksen til original array a som innhold i arrayet indeks
                    i++; // Oker indeksen
                    if (i == kopi.length) { // Om den valgte indeksen er storre enn arrayets lengde; break
                        break;
                    }
                }
            }
        }
        return indeks; // Returnerer arrayet indeks
    }

This method takes in an array, copies it, sorts the copy and matches the values in the copied array to the original, then takes those indexes and puts them in a new array.这个方法接受一个数组,复制它,对副本进行排序并将复制数组中的值与原始值匹配,然后获取这些索引并将它们放入一个新数组中。 The output is the original indexes for the original array, as if the array has been sorted.输出是原始数组的原始索引,就好像数组已排序。

I have a long test file that i've not written, but it basically shows that the "tredjeMin" method returns the wrong indexes.我有一个尚未编写的长测试文件,但它基本上表明“tredjeMin”方法返回了错误的索引。 The "indekssortering" method works and returns what it's supposed to. “indekssortering”方法有效并返回它应该做的。 I dont have a main method, but when I tested it on my own, I did something like this:我没有主要方法,但是当我自己测试它时,我做了这样的事情:

int[] a = {2, 0, 8, 12, 4, 0, 13, 3, 0, 0};

        Oblig1 tester = new Oblig1();
        int[] z = tester.indekssortering(a);
        int[] u = tester.tredjeMin(a);

        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(z));
        System.out.println(Arrays.toString(u));

The array u is basically supposed to match the three first values inn z.数组 u 基本上应该匹配 inn z 的三个第一个值。

I have worked on this all day, and I'm basically blind to whats wrong.我一整天都在研究这个,我基本上对出了什么问题视而不见。 Thank you so much in advance!!非常感谢您提前!

Edits: adding info编辑:添加信息

I'm having a little bit of a hard time understanding your code since the variables/methods/comments are in norwegian, but I think your method for find the three smallest indexes could be simplified a lot by following the below pseudocode.由于变量/方法/注释是挪威语,我很难理解您的代码,但我认为通过遵循以下伪代码可以大大简化您查找三个最小索引的方法。 By simplifying it, it will be easier to debug.通过简化它,调试会更容易。

The first difference I made is, rather than trying to keep the three min indexes sorted the entire time, just sort them at the end.我所做的第一个区别是,与其试图保持三个最小索引一直排序,不如在最后对它们进行排序。

The second is to just track the indexes instead of the indexes and the values.第二个是只跟踪索引而不是索引和值。 To get the value, just check the array at that index.要获取该值,只需检查该索引处的数组。

for (int i = 0; i < a.length(); i++) {
    if (a[i] < max(a[m1], a[m2], a[m3])) { // you have to implement max()
        // replace max of m1, m2, m3 with i
    }
}

int[] result = new int[] {m1, m2, m3)

// sort the array of 3

Based on your comments - You don't need the tredjeMin method at all.根据您的评论 - 您根本不需要tredjeMin方法。

You can just do following to validate -您只需执行以下操作即可验证-

int[] a ={2, 0, 8, 12, 4, 0, 13, 3, 0, 0};
int sortedIndex[] = indekssortering(a);
System.out.println("Min 3 Indexes - " + sortedIndex[0]+" , "+ sortedIndex[1]+" , "+ sortedIndex[2]);

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

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