代码之家  ›  专栏  ›  技术社区  ›  Ed.

如何按值排列数组(排序)*扭曲*

  •  5
  • Ed.  · 技术社区  · 18 年前

    我想使用 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 回复  |  直到 6 年前
        1
  •  7
  •   Zooba Necrolis    18 年前

    既然你使用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;
    }
    

    希望这足够清楚。

        2
  •  3
  •   Maciej Hehl    18 年前

    好的,这是我在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;
    }
    
        3
  •  2
  •   Marius    18 年前

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

    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;
    }
    
        4
  •  1
  •   aravinth    10 年前

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

    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]++;
    

    每个元素的秩将为秩[i]+1,顺序为1,2,….n

        5
  •  0
  •   Patrick    18 年前

    在python中:

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

    还要注意,上面的代码假定oldArray中的值是唯一的。如果不是这样,则需要进行一些后处理以解决绑定值。

        6
  •  0
  •   oz10    18 年前

       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;
    

        7
  •  0
  •   Phillip Kigenyi    9 年前

    这是一个c语言的解决方案

    #include <stdio.h>
    
    void swap(int *xp, int *yp) {
        int temp = *xp;
        *xp = *yp;
        *yp = temp;
    }
    
    // A function to implement bubble sort
    void bubbleSort(int arr[], int n) {
        int i, j;
        for (i = 0; i < n - 1; i++)
    
            // Last i elements are already in place
            for (j = 0; j < n - i - 1; j++)
                if (arr[j] > arr[j + 1])
                    swap(&arr[j], &arr[j + 1]);
    }
    
    /* Function to print an array */
    void printArray(int arr[], int size) {
        for (int i = 0; i < size; i++)
            printf("%d ", arr[i]);
        printf("\n");
    }
    
    int main() {
        int arr[] = {64, 34, 25, 12, 22, 11, 98};
        int arr_original[] = {64, 34, 25, 12, 22, 11, 98};
        int rank[7];
    
        int n = sizeof(arr) / sizeof(arr[0]);
        bubbleSort(arr, n);
    
        printf("Sorted array: \n");
        printArray(arr, n);
    
        //PLACE RANK
        //look for location of number in original array
        //place the location in rank array
        int counter = 1;
        for (int k = 0; k < n; k++){
            for (int i = 0; i < n; i++){
                printf("Checking..%d\n", i);
                if (arr_original[i] == arr[k]){
                    rank[i] = counter;
                    counter++;
                    printf("Found..%d\n", i);
                }
            }
        }
    
        printf("Original array: \n");
        printArray(arr_original, n);
    
        printf("Rank array: \n");
        printArray(rank, n);
        return 0;
    }