code_junkie

How does one rank an array (sort) by value? *With a twist*

Nov 10th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. void SortArray( int ** pArray, int ArrayLength ) {
  2.  
  3. int i, j, flag = 1; // set flag to 1 to begin initial pass
  4. int * temp; // holding variable orig with no *
  5. for(i = 1; (i <= ArrayLength) && flag; i++)
  6. {
  7. flag = 0;
  8. for (j=0; j < (ArrayLength -1); j++)
  9. {
  10. if (*pArray[j+1] > *pArray[j]) // ascending order simply changes to <
  11. {
  12. &temp = &pArray[j]; // swap elements
  13. &pArray[j] = &pArray[j+1]; //the problem lies somewhere in here
  14. &pArray[j+1] = &temp;
  15. flag = 1; // indicates that a swap occurred.
  16. }
  17. }
  18. }
  19. };
  20.  
  21. int* intArray; // set somewhere else
  22. int arrayLen; // set somewhere else
  23.  
  24. int** pintArray = new int*[arrayLen];
  25. for(int i = 0; i < arrayLen; ++i)
  26. {
  27. pintArray[i] = &intArray[i];
  28. }
  29.  
  30. // This function sorts the pointers according to the values they
  31. // point to. In effect, it sorts intArray without losing the positional
  32. // information.
  33. SortIntPointers(pintArray, arrayLen);
  34.  
  35. // Dereference the pointers and assign their sorted position.
  36. for(int i = 0; i < arrayLen; ++i)
  37. {
  38. *pintArray[i] = i;
  39. }
  40.  
  41. function bubbleRank(A){
  42. var B = new Array();
  43. for(var i=0; i<A.length; i++){
  44. B[i] = i;
  45. }
  46. do{
  47. swapped = false;
  48. for(var i=0; i<A.length; i++){
  49. if(A[B[i]] > A[B[i+1]]){
  50. var temp = B[i];
  51. B[i] = B[i+1];
  52. B[i+1] = temp;
  53. swapped = true;
  54. }
  55. }
  56. }while(swapped);
  57. return B;
  58. }
  59.  
  60. #include <iostream>
  61. #include <algorithm>
  62.  
  63. struct mycomparison
  64. {
  65. bool operator() (int* lhs, int* rhs) {return (*lhs) < (*rhs);}
  66. };
  67.  
  68. int main(int argc, char* argv[])
  69. {
  70. int myarray[] = {1, 3, 6, 2, 4, 9, 5, 12, 10};
  71. const size_t size = sizeof(myarray) / sizeof(myarray[0]);
  72. int *arrayofpointers[size];
  73. for(int i = 0; i < size; ++i)
  74. {
  75. arrayofpointers[i] = myarray + i;
  76. }
  77. std::sort(arrayofpointers, arrayofpointers + size, mycomparison());
  78. for(int i = 0; i < size; ++i)
  79. {
  80. *arrayofpointers[i] = i + 1;
  81. }
  82. for(int i = 0; i < size; ++i)
  83. {
  84. std::cout << myarray[i] << " ";
  85. }
  86. std::cout << std::endl;
  87. return 0;
  88. }
  89.  
  90. newArray = sorted(oldArray)
  91. blankArray = [0] * len(oldArray)
  92. for i in xrange(len(newArray)):
  93. dex = oldArray.index(newArray[i])
  94. blankArray[dex] = i
  95.  
  96. std::vector<int> intVector;
  97. std::vector<int> rank;
  98.  
  99. // set up values according to your example...
  100. intVector.push_back( 1 );
  101. intVector.push_back( 3 );
  102. intVector.push_back( 4 );
  103. intVector.push_back( 9 );
  104. intVector.push_back( 6 );
  105.  
  106.  
  107. for( int i = 0; i < intVector.size(); ++i )
  108. {
  109. rank.push_back( i );
  110. }
  111.  
  112. using namespace boost::lambda;
  113. std::sort(
  114. rank.begin(), rank.end(),
  115. var( intVector )[ _1 ] < var( intVector )[ _2 ]
  116. );
  117.  
  118. //... and because you wanted to replace the values of the original with
  119. // their rank
  120. intVector = rank;
Add Comment
Please, Sign In to add comment