如何对数组进行排序(索引)以使用这些索引将原始数组从最小值排序到最大值 [英] how to sort (indexes) of an array to get the original array sorted from the smallest to the biggest value by using those indexes

查看:173
本文介绍了如何对数组进行排序(索引)以使用这些索引将原始数组从最小值排序到最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有这个数组

int[] a = {6,10,16,11,7,12,3,9,8,5};

我想对这样的索引进行排序

i want to sort its indexes like this

[6,9,0,4,8,7,1,3,5,2]

因此我可以使用索引从最小值到最大值进行排序。
在我的代码中我得到了这个

so I can use indexes to sort a from the smallest to the biggest value. in my code I got instead this

[6, 9, 4, 8, 7, 4, 5, 6, 6, 6] 

这是我的代码

int[] a = {6,10,16,11,7,12,3,9,8,5};
int[] indeks = indekssortering(a);
System.out.println(Arrays.toString(indeks));

public static int[] indekssortering(int[] a){
    int[] indeks = new int[a.length];
    int m = 0;
    boolean finnes = false;
    boolean nyVerdi = false;
    int n = 0;
    for (int j = 0; j < a.length; j++) {
        for (int i = m+1; i < a.length ; i++) {
            if(a[m] > a[i]){
                for (int k = 0; k < j; k++) {
                    if(indeks[k] == i) finnes = true; //check if the same position is saved before
                }
                if(!finnes){ // if not so its the next minimum value
                    m = i;
                } else {
                    nyVerdi = true; // if didnt find match then the value used to compare is the next minimum
                }
            }
            finnes = false;
        }
        indeks[j] = m;
        if(nyVerdi) n=n+1;
        nyVerdi = false;
        m=0+n;
    }
    return indeks;
}

我需要帮助才能使这段代码有效或找到一个比这更好的想法。

I need help to make this code work or to find a better idea than this.

我试图做的是。
将所有值与第一个值进行比较,得到最小值并将位置保存到数组中(凹凸)。在保存之前,我做了循环以检查之前是否添加了这个位置。如果没有大于用于比较的值的值,则意味着它的下一个小值。
我得到了一些正确的,其他人错了。我相信我需要改变这个想法并找到更好的解决方案。

what I tried to do is. compare all values with the first values, get the smallest and save the position into the array (indeks). before save it, I made for loop to check if this position is been added before. and if there is no value bigger than the value used to compare that means its the next small value. i got some of them right and others wrong. I believe that I need to change this idea and find a better solution.

推荐答案

这里有经典的冒泡排序算法实现修改为排序索引

Here a classical bubble sort algorithm implementation modified to sort indexes

您正在寻找的是实际上排序 int的任何排序算法[] array 。它是遍布互联网的无穷无尽的实施列表。然后只需将比较更改为数组[result [i]] 并交换结果中的值而不是 array itseft。

What you are looking for is actually any sorting algorithm which sorts int [] array. It's endless list of implementations all over the Internet. And then just change the comparison to array[result[i]] and swap values in result not in array itseft.

static int[] sort(int[] array) {
    final int size = array.length;

    final int[] result = new int[size];
    for (int i = 0; i < size; i++)
        result[i] = i;

    boolean sorted;
    do {
        sorted = true;
        int bubble = result[0];
        for (int i = 0; i < size - 1; i++) {
            if (array[bubble] > array[result[i + 1]]) {
                result[i] = result[i + 1];
                result[i + 1] = bubble;
                sorted = false;
            } else {
                bubble = result[i + 1];
            }
        }
    } while (!sorted);

    return result;
}

result arrays for your input data is [6, 9, 0, 4, 8, 7, 1, 3, 5, 2]

这篇关于如何对数组进行排序(索引)以使用这些索引将原始数组从最小值排序到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆