一个人如何通过排序值的数组(排序)? *捻度* [英] How does one rank an array (sort) by value? *With a twist*

查看:120
本文介绍了一个人如何通过排序值的数组(排序)? *捻度*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个数组,它在C / C ++进行排序,以其'等级,即对应于排序后的位置的整数替换每个值。下面有一个例子:输入:1,3,4,9,6。输出:1,2,3,5,4。所以,我需要相对于所有其它的值的位置,以替换各个值。我希望是有道理的,但它晚了,所以谁知道:P

编辑:我使用的是希尔排序的过程,重复'的行列是任意选择,在此基础上首先出现原始数组中

更新:

尽管我尽了最大努力,我还没有能够实现的排序由它们指向的值pointerse数组的排序算法。可能有人请告诉我怎么回事错了吗?当前示例不会编译,我已经改变了它周围这么多,它不真正的问题。我倒是很AP preciate,如果有人可以帮助我解决这个问题!

 无效SortArray(INT **粒子阵列,诠释ArrayLength){    INT I,J,标志= 1; //设置标志1开始初始通
    INT *温度; //控股变量原稿无*
    为(ⅰ= 1;(I&下; = ArrayLength)及&放大器;标志;我+ +)
    {
        标志= 0;
        为(J = 0; J&≤(ArrayLength -1); J ++)
        {
            如果(*粒子阵列[J + 1] GT; *粒子阵列[J])//升序只是改变到<
            {
                &安培; TEMP =&放大器;粒子阵列[J]。 //交换元素
                &安培;粒子阵列[J] =安培;粒子阵列[J + 1]; //问题就出在这里的某个地方
                &安培;粒子阵列[J + 1] =&放大器;温度;
                标志= 1; //表明发生了互换。
            }
        }
    }
};


解决方案

由于您使用C ++,我会做这样的事情。在 SortIntPointers 函数可以是任何类型的算法,重要的是,它排序基于指针数组 INT 它们都指向。一旦做到这一点,你可以通过指针数组,并指定其排序索引将在原始数组中的原始位置结束。

 为int * intArray; //设置别的地方
INT arrayLen; //设置别的地方INT ** pintArray = INT新* [arrayLen]
的for(int i = 0; I< arrayLen ++ I)
{
    pintArray [I] =安培; intArray [I]
}//这个函数根据值排序,他们的指针
// 指向。实际上,它排序intArray不失位置
//信息。
SortIntPointers(pintArray,arrayLen);//取消引用指针和分配他们的排序位置。
的for(int i = 0; I< arrayLen ++ I)
{
    * pintArray [我] =我;
}

我希望这是不够清楚。

I need to take an array and sort it in c/c++, replacing each value with its' "rank", an integer that corresponds to its position after sort. Heres an example: Input: 1,3,4,9,6. Output: 1, 2, 3, 5, 4. So, I need to replace each value with its position relative to all the other values. I hope that makes sense, but its late, so who knows :p.

Edit: I am using a shell sort procedure, and duplicates' ranks are arbitrarily chosen, based on which came first in the original array.

Update:

Despite my best efforts, I havent been able to implement a sort algorithm that sorts an array of pointerse by the values they point to. Could someone please tell me whats going wrong? The current example wont compile, and I've changed it around so much that it doesnt really matter. I'd very much appreciate it if someone could help me fix this!

void SortArray( int ** pArray, int ArrayLength ) {

    int i, j, flag = 1;    // set flag to 1 to begin initial pass
    int * temp;    // holding variable orig with no *
    for(i = 1; (i <= ArrayLength) && flag; i++)
    {
        flag = 0;
        for (j=0; j < (ArrayLength -1); j++)
        {
            if (*pArray[j+1] > *pArray[j])    // ascending order simply changes to <
            { 
                &temp = &pArray[j];    // swap elements
                &pArray[j] = &pArray[j+1];    //the problem lies somewhere in here
                &pArray[j+1] = &temp;
                flag = 1;    // indicates that a swap occurred.
            }
        }
    }
};

解决方案

Since you're using C++, I would do it something like this. The SortIntPointers function can be any sort algorithm, the important part is that it sorts the array of pointers based on the int that they are pointing to. Once that is done, you can go through the array of pointers and assign their sorted index which will end up in the original position in the original array.

int* intArray; // set somewhere else
int arrayLen;  // set somewhere else  

int** pintArray = new int*[arrayLen];
for(int i = 0; i < arrayLen; ++i)
{
    pintArray[i] = &intArray[i];
}

// This function sorts the pointers according to the values they
// point to. In effect, it sorts intArray without losing the positional
// information.
SortIntPointers(pintArray, arrayLen);

// Dereference the pointers and assign their sorted position.
for(int i = 0; i < arrayLen; ++i)
{
    *pintArray[i] = i;
}

Hopefully that's clear enough.

这篇关于一个人如何通过排序值的数组(排序)? *捻度*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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