Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- }
- }
- }
- };
- 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;
- }
- 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;
- }
- #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;
- }
- newArray = sorted(oldArray)
- blankArray = [0] * len(oldArray)
- for i in xrange(len(newArray)):
- dex = oldArray.index(newArray[i])
- blankArray[dex] = i
- 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;
Add Comment
Please, Sign In to add comment