如何按值对数组(排序)进行排名? *扭曲*

时间:2008-08-17 02:13:56

标签: c++ c arrays sorting

我想使用C/C++按升序对数组进行排序。结果是包含元素索引的数组。每个索引都与排序数组中的元素位置相对应。

示例

Input:  1, 3, 4, 9, 6
Output: 1, 2, 3, 5, 4

编辑:我正在使用shell排序程序。重复值索引是根据原始数组中的第一个重复值任意选择的。

更新

尽管我付出了最大的努力,但我还是无法为指针数组实现排序算法。目前的例子不会编译。

有人可以告诉我出了什么问题吗?

我非常感谢你的帮助!

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.
            }
        }
    }
};

7 个答案:

答案 0 :(得分:7)

由于您使用的是C ++,我会这样做。 SortIntPointers函数可以是任何排序算法,重要的是它根据指向的int对指针数组进行排序。完成后,您可以遍历指针数组并指定它们的排序索引,这些索引将最终位于原始数组中的原始位置。

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;
}

希望这很清楚。

答案 1 :(得分:3)

好的,这是我在C ++中的尝试

#include <iostream>
#include <algorithm>

struct mycomparison
{
    bool operator() (int* lhs, int* rhs) {return (*lhs) < (*rhs);}
};

int main(int argc, char* argv[])
{
    int myarray[] = {1, 3, 6, 2, 4, 9, 5, 12, 10};
    const size_t size = sizeof(myarray) / sizeof(myarray[0]);
    int *arrayofpointers[size];
    for(int i = 0; i < size; ++i)
    {
        arrayofpointers[i] = myarray + i;
    }
    std::sort(arrayofpointers, arrayofpointers + size, mycomparison());
    for(int i = 0; i < size; ++i)
    {
        *arrayofpointers[i] = i + 1;
    }
    for(int i = 0; i < size; ++i)
    {
        std::cout << myarray[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}

答案 2 :(得分:2)

创建一个新数组,其值从0增加到n-1(其中n是要排序的数组的长度)。然后根据新数组中的值索引的旧数组中的值对新数组进行排序。

例如,如果使用冒泡排序(易于解释),则不要比较新数组中的值,而是比较新数组中由新数组中的值索引的位置中的值:

function bubbleRank(A){
  var B = new Array();
  for(var i=0; i<A.length; i++){
    B[i] = i;
  }
  do{
    swapped = false;
    for(var i=0; i<A.length; i++){
      if(A[B[i]] > A[B[i+1]]){
        var temp = B[i];
        B[i] = B[i+1];
        B[i+1] = temp;
        swapped = true;
      }
    }
  }while(swapped);
  return B;
}

答案 3 :(得分:0)

嗯,有一个trival n ^ 2解决方案。

在python中:

newArray = sorted(oldArray)
blankArray = [0] * len(oldArray)
for i in xrange(len(newArray)):
  dex = oldArray.index(newArray[i])
  blankArray[dex]  = i

根据列表的大小,这可能会有效。如果你的列表很长,你需要做一些奇怪的并行数组排序,这看起来不是很有趣,并且是在代码中引入额外错误的快速方法。

另请注意,上面的代码假定oldArray中有唯一值。如果情况并非如此,则需要进行一些后期处理以解决绑定值。

答案 4 :(得分:0)

使用boost :: lambda ...

并行排序矢量
   std::vector<int> intVector;
   std::vector<int> rank;

   // set up values according to your example...
   intVector.push_back( 1 );
   intVector.push_back( 3 );
   intVector.push_back( 4 );
   intVector.push_back( 9 );
   intVector.push_back( 6 );


   for( int i = 0; i < intVector.size(); ++i )
   {
      rank.push_back( i );
   }

   using namespace boost::lambda;
   std::sort( 
              rank.begin(), rank.end(),
              var( intVector )[ _1 ] < var( intVector )[ _2 ] 
            );

   //... and because you wanted to replace the values of the original with 
   //    their rank
   intVector = rank;

注意:我使用vectorS而不是数组,因为它更清晰/更容易,而且,我使用的是C风格的索引,它从0开始计数,而不是1。

答案 5 :(得分:0)

创建一个新数组并使用冒泡排序对元素进行排名

int arr[n];
int rank[n];
 for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
       if(arr[i]>arr[j])
         rank[i]++;

每个元素的等级将是rank [i] +1,大约为1,2,.... n

答案 6 :(得分:0)

这是c语言的解决方案

{Token::getToken()}